Is a structure or datetime class in C #?

I just started C #. But I can not understand DateTime . I know this is a structure, but why do I see different ways to initialize it as a class.

How is it if it's a structure?

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

So there are two versions of datetime in C # one is struct and the other is a class?

+7
c # datetime
source share
7 answers

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

The difference between the two is that the first one copies the value inside the static property called Now , and the second initializes the value through one of the DateTime 11 constructors.

+8
source share

Using the new keyword does not mean that it creates an instance of the class. It creates an instance of the structure. Structures can also have constructors, and they are initialized (in C #) using the same syntax as classes, even though they are structures.

+8
source share

DateTime is a structure. And structures can 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(); 
+4
source share

This is really a structure. To find out what type is something, you can do two things:

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

Structures behave basically like classes, you can create them using the new operator, and they can also have methods. You cannot use the way they are created, as a way to determine if something is a structure or a class.

+3
source share

System.DateTime is a struct .

This does not mean that it cannot have many different constructors, methods, and overloads.

+2
source share

DateTime is a structure: MSDN Struct .aspx documentation . Structures may have design overloads.

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

+1
source share

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

-3
source share

All Articles