简体   繁体   English

.NET Framework中的公共静态DateTime ToDateTime(DateTime value)是什么目的?

[英]What is the purpose of : public static DateTime ToDateTime(DateTime value) in the .NET Framework?

I am maintaining an existing project, and I found this line of code: 我正在维护一个现有项目,并且发现以下代码行:

Datetime someDate = ....;
var anotherDateTime = Convert.ToDateTime(someDate);

At first, I expected that someDate is converted to a string by calling the ToString method on it implicitly, but I was wrong. 起初,我预计someDate是通过调用转换为字符串ToString方法就可以了含蓄,但我错了。 As I pressed F12 on the method I see the definition of that method in the System.Convert class, which is like the following: 当我在方法上按F12时,在System.Convert类中看到该方法的定义,如下所示:

// Summary: Returns the specified System.DateTime object; no actual conversion is performed.
// Parameters:
//   value: A date and time value.
// Returns:
//     value is returned unchanged.
public static DateTime ToDateTime(DateTime value);

First Why does the .NET framework have a method like this in the first place, as the documentation says that this method does Nothing? 首先为什么.NET框架首先要有这样的方法,如文档所述,该方法什么都没有?
Second While I am refactoring the code, can I safely remove the call for this method without affecting the behavior? 其次,在重构代码时,是否可以安全地删除对此方法的调用而不会影响行为?

It is because the Convert class is intented to work with types that implement the IConvertible interface. 这是因为Convert类旨在与实现IConvertible接口的类型一起使用。

This interface contains methods to convert an implementing type to CLR types like decimal , byte , DateTime etc. Each of those types implement IConvertible themselves. 此接口包含将实现类型转换为CLR类型(例如, decimalbyteDateTime等)的方法。这些类型中的每一个都自己实现IConvertible

So, Convert.ToDateTime(DateTime d) isn't the only method that does "nothing". 因此, Convert.ToDateTime(DateTime d)并不是唯一不执行任何操作的方法。 It exists for any of those CLR types implementing IConvertible as well, eg Convert.ToChar(char c) . 对于任何实现IConvertible CLR类型,它都存在,例如Convert.ToChar(char c) It just comes from the fact that all of these types implement IConvertible . 这仅仅是因为所有这些类型都实现了IConvertible

You can read more about this in the comments of the source code of the Convert class. 您可以在Convert类的源代码注释中阅读有关此内容的更多信息。

Yes, you can remove it. 是的,您可以将其删除。 It is completely redundant. 这是完全多余的。 As for why: I guess someone thought it was a good idea to avoid people accidentally calling Convert.ToDateTime(object) (which would be implicit) - which would involve a box, type-check and unbox. 至于原因:我有人认为避免人们意外调用Convert.ToDateTime(object) (这是隐式的)是一个好主意-这涉及到box,type-check和unbox。 Frankly that seems like a silly reason to me, along the lines of "write bad code, get bad results". 坦白说,这对我来说似乎是一个愚蠢的理由,类似于“编写不良代码,取得不良结果”。

As you can see in the current BCL sources : 您可以在当前的BCL来源中看到:

public static DateTime ToDateTime(DateTime value) {
        return value;
}

there is no actual conversion, so you can safely remove those calls. 没有实际的转换,因此您可以放心删除这些通话。

Though I agree with answers here so far, I guess there is also another aspect to it, which is code generation. 尽管到目前为止我都同意答案,但是我想它还有另一个方面,就是代码生成。 In the early days of .NET, code generation was very often done using CodeDOM (and sometimes still is, eg with the WinForms Designer). 在.NET的早期,代码生成通常是使用CodeDOM进行的(有时仍是使用WinForms Designer进行的)。 CodeDOM does not really track the type of variables as this type might not be known at the time of code generation (for example if the type is also generated). CodeDOM不会真正跟踪变量的类型,因为在代码生成时可能不知道这种类型(例如,如果还生成了类型)。 Therefore, it is a lot easier to just generate a reference to a method ToDate and have the compiler figure out which of the overloads to use. 因此,仅生成对方法ToDate的引用并让编译器找出要使用的重载要容易ToDate Given that these methods are non-virtual, they can be inlined and there is not even a performance penalty. 由于这些方法是非虚拟的,因此可以内联它们,甚至不会造成性能损失。

That said, I am pretty sure the WinForms Designer code generator does not use this method, at least not in .NET 2.0, the earliest version that I have worked with. 也就是说,我非常确定WinForms Designer代码生成器不会使用此方法,至少在我使用的最早版本.NET 2.0中不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM