locked
what is the difference between implementation of DateTime.Now method ?? RRS feed

  • Question

  • i created a new instance as follows DateTime mydate=new DateTime(); , now when i am trying to return the current date by using mydate instance , i am not getting mydate.Now property , but i am getting "DateTime.Now" perfectly , i want to know what is the difference between 2 implementations & why the Now property not available with mydate object.??

    Saturday, November 30, 2013 4:12 PM

Answers

  • If you are looking at Microsoft class libraries, like the .NET framework, look it up on MSDN. Note that the individual members of a class can be declared as static while the class itself is non-static, which is the case with DateTime.

    - HomeGrownCoder My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    • Marked as answer by Anne Jing Monday, December 9, 2013 1:36 AM
    Sunday, December 1, 2013 5:15 AM

All replies

  • The .Now property of the DateTime class is static, which means there is only one copy of the property instead of a copy per instance of the class. There would be no advantage to having a copy on each instance as they would all return the same value since the value of .Now is independent of the other properties of the DateTime class. Hope this makes more sense now.

    - HomeGrownCoder My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    • Proposed as answer by Dave SmitsMVP Saturday, November 30, 2013 11:41 PM
    Saturday, November 30, 2013 4:25 PM
  • how can i identify whether a particular class is static??
    Sunday, December 1, 2013 4:17 AM
  • If you are looking at Microsoft class libraries, like the .NET framework, look it up on MSDN. Note that the individual members of a class can be declared as static while the class itself is non-static, which is the case with DateTime.

    - HomeGrownCoder My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    • Marked as answer by Anne Jing Monday, December 9, 2013 1:36 AM
    Sunday, December 1, 2013 5:15 AM