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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












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?










share|improve this question

















  • 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














up vote
6
down vote

favorite
1












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?










share|improve this question

















  • 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












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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












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".






share|improve this answer



























    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.






    share|improve this answer



























      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.






      share|improve this answer





























        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.






        share|improve this answer




















          Your Answer





          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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".






          share|improve this answer
























            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".






            share|improve this answer






















              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".






              share|improve this answer












              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".







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 1 hour ago









              Marc Gravell♦

              763k18921012520




              763k18921012520






















                  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.






                  share|improve this answer
























                    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.






                    share|improve this answer






















                      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.






                      share|improve this answer












                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 56 mins ago









                      dymanoid

                      6,79921945




                      6,79921945




















                          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.






                          share|improve this answer


























                            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.






                            share|improve this answer
























                              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.






                              share|improve this answer














                              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.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 46 mins ago

























                              answered 52 mins ago









                              Adrian

                              8,53811436




                              8,53811436




















                                  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.






                                  share|improve this answer
























                                    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.






                                    share|improve this answer






















                                      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.






                                      share|improve this answer












                                      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.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 36 mins ago









                                      Georg

                                      3,4011232




                                      3,4011232



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          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













































































                                          Comments

                                          Popular posts from this blog

                                          What does second last employer means? [closed]

                                          List of Gilmore Girls characters

                                          Confectionery