Convert.ToDateTime Method
Converts a specified value to a DateTime value.
This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.
| Name | Description | |
|---|---|---|
|
ToDateTime(Boolean) | Calling this method always throws InvalidCastException. |
|
ToDateTime(Byte) | Calling this method always throws InvalidCastException. |
|
ToDateTime(Char) | Calling this method always throws InvalidCastException. |
|
ToDateTime(DateTime) | Returns the specified DateTime object; no actual conversion is performed. |
|
ToDateTime(Decimal) | Calling this method always throws InvalidCastException. |
|
ToDateTime(Double) | Calling this method always throws InvalidCastException. |
|
ToDateTime(Int16) | Calling this method always throws InvalidCastException. |
|
ToDateTime(Int32) | Calling this method always throws InvalidCastException. |
|
ToDateTime(Int64) | Calling this method always throws InvalidCastException. |
|
ToDateTime(Object) | Converts the value of the specified object to a DateTime object. |
|
ToDateTime(SByte) | Calling this method always throws InvalidCastException. |
|
ToDateTime(Single) | Calling this method always throws InvalidCastException. |
|
ToDateTime(String) | Converts the specified string representation of a date and time to an equivalent date and time value. |
|
ToDateTime(UInt16) | Calling this method always throws InvalidCastException. |
|
ToDateTime(UInt32) | Calling this method always throws InvalidCastException. |
|
ToDateTime(UInt64) | Calling this method always throws InvalidCastException. |
|
ToDateTime(Object, IFormatProvider) | Converts the value of the specified object to a DateTime object, using the specified culture-specific formatting information. |
|
ToDateTime(String, IFormatProvider) | Converts the specified string representation of a number to an equivalent date and time, using the specified culture-specific formatting information. |
/* Calling this method always throws System.InvalidCastException */ is applicable for most of the marameters.
This means we could never use this methods because no application would be using these logic (to get exceptions). Why they are still present in the library?
The methods where the conversion is not supported could be removed from the library.
Thanks,
Samrat
Why Do These Methods Exist?
Besides providing methods for some specialized conversions, such as to and from Base64 strings, the Convert class is intended to provide a public implementation of the IConvertible interface for each of the primitive data types in the .NET Framework. (The IConvertible implementation of each of the primitives is private.) This means that, for each primitive type, the Convert class must provide a method to convert that type to every other primitive type, even if the conversion itself is not possible. In the case of a DateTime value, most of those type conversions are either inherently impossible (such as Convert.ToDateTime(Boolean) or Convert.ToDateTime(Char)) or would require an interpretation of a primitive value that might be inaccurate (such as Convert.ToDate(Int64) or a conversion from any other integral type, which would require that the integral value be interpreted as a number of ticks).
For more information on the Convert class and its role in type conversion for the .NET Framework, see "The Convert Class" in the Type Conversion in the .NET Framework topic at http://msdn.microsoft.com/en-us/library/98bbex99.aspx.
I hope that this helps to answer your question.
--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation
- 3/15/2012
- Samrat Bhattacharjee
- 3/26/2012
- R Petrusha - MSFT