10

I just started c#. But can not figure out DateTime. I know it is a struct but why do I see different ways of initialize it like class sometime.

How is this if it is a struct?

DateTime myValue = DateTime.Now;  // This is struct

DateTime myValue2 = new DateTime(); // This is class with +11 overloads. 

So is there two versions of datetime in c# one is struct and other is class?

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
Ozgur Erdogan
  • 1,133
  • 1
  • 8
  • 13
  • 4
    Structs can have constructors, and DateTime is only a struct – Kevin Jan 09 '14 at 22:14
  • See [**struct**](http://msdn.microsoft.com/en-us/library/ah19swz4.aspx). Read the remarks section. – crush Jan 09 '14 at 22:15
  • Also, see [What's the difference between a struct and class in .Net?](http://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net) – crush Jan 09 '14 at 22:17
  • 3
    Voting to close because the question is clearly answered by the documentation on MSDN. – evanmcdonnal Jan 09 '14 at 22:19
  • 2
    Additionally, `DateTime.Now` is just a property which returns an instance of DateTime with the current time value. Even reference types can do that. – Magus Jan 09 '14 at 22:31

7 Answers7

9

The use of the keyword new does not mean that it is creating an instance of a class. It is creating an instance of a struct. Structs can have constructors too, and they are initialized (in C#) using the same syntax as classes, despite being structs.

Servy
  • 202,030
  • 26
  • 332
  • 449
9

A type cannot be a struct and a by-reference type at the same time. Both constructs make a DateTime, which is a value type (also known as the struct).

The difference between the two is that the first produces the value to be copied inside a static property called Now, and the second initializes the value through one of DateTime's 11 constructors.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
4

DateTime is a struct. And structs might have constructors too.Take a look at this documentation If you want to be surprised, you can define an integer like this:

int x = new int();
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Technically, they are guaranteed to have their default parameterless constructor. – Erik Jan 09 '14 at 22:17
  • The link [documentation](https://msdn.microsoft.com/en-us/library/s1ax56ch.aspx) says `Structs fall into these categories` etc. Why the categories don't include DateTime? – Bigeyes Feb 07 '17 at 14:42
4

It is indeed a structure. To know which type is something, you can do two things :

1) Look at the doc, for DateTime it is clearly indicated in the title
2) Hover over the type. Visual Studio will pop a tooltip : enter image description here

Structs behave mostly like classes, you can instantiate them with the new operator and they can have methods too. You cannot use the way they are instantiated as a way to tell if something is struct or a class.

Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55
  • +1 for mentioning the tooltip. Hitting F12 on the keyword while `DateTime` is selected also takes you to the metadata. – pcnThird Jan 09 '14 at 22:34
2

System.DateTime is a struct.

Which doesn't mean it can't have many different constructors and methods and overloads.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
1

DateTime is a struct: MSDN Struct Documentation.aspx. Structs can have constructor overloads.

By the way, if you hover over DateTime with your cursor, Intellisense says struct System.DateTime.

pcnThird
  • 2,362
  • 2
  • 19
  • 33
-3

A Struct is more like a lightweight class, and a value instead of an object. A DateTime is one of those struct