What is the purpose of : public static DateTime ToDateTime(DateTime value) in the .NET Framework?
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
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. As I pressed F12 on the method I see the definition of that method in the System.Convert
class, which is like the following:
// 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?
Second While I am refactoring the code, can I safely remove the call for this method without affecting the behavior?
c# .net datetime
add a comment |Â
up vote
10
down vote
favorite
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. As I pressed F12 on the method I see the definition of that method in the System.Convert
class, which is like the following:
// 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?
Second While I am refactoring the code, can I safely remove the call for this method without affecting the behavior?
c# .net datetime
3
Common in early .NET versions to implement late binding, before Linq, EF and dynamic made the job easier. Mapping data from a dbase table row for example, the dbase provider might supply a DateTime based on the column type. Omitting that Convert method would make such code too painful.
– Hans Passant
2 hours ago
add a comment |Â
up vote
10
down vote
favorite
up vote
10
down vote
favorite
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. As I pressed F12 on the method I see the definition of that method in the System.Convert
class, which is like the following:
// 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?
Second While I am refactoring the code, can I safely remove the call for this method without affecting the behavior?
c# .net datetime
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. As I pressed F12 on the method I see the definition of that method in the System.Convert
class, which is like the following:
// 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?
Second While I am refactoring the code, can I safely remove the call for this method without affecting the behavior?
c# .net datetime
c# .net datetime
edited 8 mins ago


Boann
36k1285116
36k1285116
asked 3 hours ago


Hakam Fostok
4,83383561
4,83383561
3
Common in early .NET versions to implement late binding, before Linq, EF and dynamic made the job easier. Mapping data from a dbase table row for example, the dbase provider might supply a DateTime based on the column type. Omitting that Convert method would make such code too painful.
– Hans Passant
2 hours ago
add a comment |Â
3
Common in early .NET versions to implement late binding, before Linq, EF and dynamic made the job easier. Mapping data from a dbase table row for example, the dbase provider might supply a DateTime based on the column type. Omitting that Convert method would make such code too painful.
– Hans Passant
2 hours ago
3
3
Common in early .NET versions to implement late binding, before Linq, EF and dynamic made the job easier. Mapping data from a dbase table row for example, the dbase provider might supply a DateTime based on the column type. Omitting that Convert method would make such code too painful.
– Hans Passant
2 hours ago
Common in early .NET versions to implement late binding, before Linq, EF and dynamic made the job easier. Mapping data from a dbase table row for example, the dbase provider might supply a DateTime based on the column type. Omitting that Convert method would make such code too painful.
– Hans Passant
2 hours ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
9
down vote
accepted
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. Frankly that seems like a silly reason to me, along the lines of "write bad code, get bad results".
add a comment |Â
up vote
4
down vote
It is because the Convert
class is intented to work with types that implement the IConvertible
interface.
This interface contains methods to convert types to decimal
, byte
, DateTime
etc. Convert.ToDateTime(DateTime d)
isn't the only method that does "nothing". It exists for any type implementing IConvertible
as well, e.g. Convert.ToChar(char c)
. It just comes from the fact that all of these types implement IConvertible
.
You can read more about this in the comments of the source code of the Convert
class.
add a comment |Â
up vote
3
down vote
As you can see in the current BCL sources:
public static DateTime ToDateTime(DateTime value)
return value;
there is no actual conversion, so you can safely remove those calls.
add a comment |Â
up vote
0
down vote
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, e.g. with the 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). 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. 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.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
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. Frankly that seems like a silly reason to me, along the lines of "write bad code, get bad results".
add a comment |Â
up vote
9
down vote
accepted
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. Frankly that seems like a silly reason to me, along the lines of "write bad code, get bad results".
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
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. Frankly that seems like a silly reason to me, along the lines of "write bad code, get bad results".
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. Frankly that seems like a silly reason to me, along the lines of "write bad code, get bad results".
answered 3 hours ago


Marc Gravell♦
763k18921012520
763k18921012520
add a comment |Â
add a comment |Â
up vote
4
down vote
It is because the Convert
class is intented to work with types that implement the IConvertible
interface.
This interface contains methods to convert types to decimal
, byte
, DateTime
etc. Convert.ToDateTime(DateTime d)
isn't the only method that does "nothing". It exists for any type implementing IConvertible
as well, e.g. Convert.ToChar(char c)
. It just comes from the fact that all of these types implement IConvertible
.
You can read more about this in the comments of the source code of the Convert
class.
add a comment |Â
up vote
4
down vote
It is because the Convert
class is intented to work with types that implement the IConvertible
interface.
This interface contains methods to convert types to decimal
, byte
, DateTime
etc. Convert.ToDateTime(DateTime d)
isn't the only method that does "nothing". It exists for any type implementing IConvertible
as well, e.g. Convert.ToChar(char c)
. It just comes from the fact that all of these types implement IConvertible
.
You can read more about this in the comments of the source code of the Convert
class.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
It is because the Convert
class is intented to work with types that implement the IConvertible
interface.
This interface contains methods to convert types to decimal
, byte
, DateTime
etc. Convert.ToDateTime(DateTime d)
isn't the only method that does "nothing". It exists for any type implementing IConvertible
as well, e.g. Convert.ToChar(char c)
. It just comes from the fact that all of these types implement IConvertible
.
You can read more about this in the comments of the source code of the Convert
class.
It is because the Convert
class is intented to work with types that implement the IConvertible
interface.
This interface contains methods to convert types to decimal
, byte
, DateTime
etc. Convert.ToDateTime(DateTime d)
isn't the only method that does "nothing". It exists for any type implementing IConvertible
as well, e.g. Convert.ToChar(char c)
. It just comes from the fact that all of these types implement IConvertible
.
You can read more about this in the comments of the source code of the Convert
class.
edited 2 hours ago
answered 2 hours ago
Adrian
8,56811436
8,56811436
add a comment |Â
add a comment |Â
up vote
3
down vote
As you can see in the current BCL sources:
public static DateTime ToDateTime(DateTime value)
return value;
there is no actual conversion, so you can safely remove those calls.
add a comment |Â
up vote
3
down vote
As you can see in the current BCL sources:
public static DateTime ToDateTime(DateTime value)
return value;
there is no actual conversion, so you can safely remove those calls.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
As you can see in the current BCL sources:
public static DateTime ToDateTime(DateTime value)
return value;
there is no actual conversion, so you can safely remove those calls.
As you can see in the current BCL sources:
public static DateTime ToDateTime(DateTime value)
return value;
there is no actual conversion, so you can safely remove those calls.
answered 2 hours ago


dymanoid
6,83921945
6,83921945
add a comment |Â
add a comment |Â
up vote
0
down vote
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, e.g. with the 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). 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. 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.
add a comment |Â
up vote
0
down vote
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, e.g. with the 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). 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. 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.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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, e.g. with the 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). 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. 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.
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, e.g. with the 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). 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. 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.
answered 2 hours ago
Georg
3,4011232
3,4011232
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52851652%2fwhat-is-the-purpose-of-public-static-datetime-todatetimedatetime-value-in-th%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Common in early .NET versions to implement late binding, before Linq, EF and dynamic made the job easier. Mapping data from a dbase table row for example, the dbase provider might supply a DateTime based on the column type. Omitting that Convert method would make such code too painful.
– Hans Passant
2 hours ago