What is the purpose of : public static DateTime ToDateTime(DateTime value) in .NET Framework?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I am maintaining an existing project, and I found this line of code
Datetime someDate = ....;
var anotherDateTime = Convert.ToDateTime(someDate);
At the first, I expected that the someDate
is converted to string by calling 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 the .NET framework has 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
6
down vote
favorite
I am maintaining an existing project, and I found this line of code
Datetime someDate = ....;
var anotherDateTime = Convert.ToDateTime(someDate);
At the first, I expected that the someDate
is converted to string by calling 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 the .NET framework has 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
1
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
43 mins ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I am maintaining an existing project, and I found this line of code
Datetime someDate = ....;
var anotherDateTime = Convert.ToDateTime(someDate);
At the first, I expected that the someDate
is converted to string by calling 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 the .NET framework has 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 the first, I expected that the someDate
is converted to string by calling 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 the .NET framework has 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
asked 1 hour ago


Hakam Fostok
4,81183560
4,81183560
1
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
43 mins ago
add a comment |Â
1
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
43 mins ago
1
1
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
43 mins 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
43 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
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
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
1
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
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
6
down vote
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
6
down vote
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
6
down vote
up vote
6
down vote
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 1 hour ago


Marc Gravell♦
763k18921012520
763k18921012520
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 56 mins ago


dymanoid
6,79921945
6,79921945
add a comment |Â
add a comment |Â
up vote
1
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
1
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
1
down vote
up vote
1
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 46 mins ago
answered 52 mins ago
Adrian
8,53811436
8,53811436
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 36 mins 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-n%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
1
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
43 mins ago