Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DateTime always create new object?

Tags:

c#

datetime

why in C# my two variables points to different DateTime objects?

DateTime a1 = DateTime.Now;
DateTime a2 = a1;

a1 = a1 + TimeSpan.FromMinutes(15);
a2 = a2 - TimeSpan.FromMinutes(16);

I realized that a2 actually points to a new object which is different from a1.

But In other case. Say i have a class Person, and age = 1;

Person a1 = new Person();
a2 = a1;
a2 = Person.Age = 2;

In the Person Case, a1 and a2 are pointing to same object. I am really confused here, anyone can explain?

like image 810
retide Avatar asked Oct 09 '12 19:10

retide


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

DateTime is a value type - a structure.

With value types, when you do something like:

DateTime a2 = a1;

a2 gets a copy of the values of a1. It is not the same reference to the same memory location, but a complete, separate copy.

On the other hand, Person is a reference type - a class.

When you do:

Person p2 = p1;

With a reference type, the reference that p2 points to is the same one that p1 points to. So changes in one are changes to both.

See Value Types and Reference Types on MSDN.

like image 197
Oded Avatar answered Sep 19 '22 14:09

Oded


As others have already pointed out DateTime is a struct, not a class and therefore a value type. You can visualize this in the Visual Studio editor, if you change the text color used to display structs. Open the dialog in menu Tools > Options and navigate to Environment > Fonts and Colors

enter image description here

It is helpful to change the color of delegates, enums, interfaces and structs (Value types).

enter image description here

In Visual Studio 2019, you can also change the look of User Members like constants or parameters.

like image 20
Olivier Jacot-Descombes Avatar answered Sep 21 '22 14:09

Olivier Jacot-Descombes