Whether DateTime in C # is a value type or a reference type

Source: Internet
Author: User

Recently encountered doubts about whether DateTime is a value type or a reference type, take a closer look at datetime-related content

Conclusion: DateTime is a value type because DateTime is a struct, and struct inherits from Syste.valuetype, which is a value type

First, the discovery of whether DateTime is a value type or a reference type

Second, understand the datetime structure body

How are DateTime.Now and Datetime.utcnow calculated?

First, the discovery of whether DateTime is a value type or a reference type

1. Write the test code first

Assign dateTime1 to dateTime2, then change the value of dateTime1, and if the value of dateTime2 is changed, then DateTime is the reference type, see the result:

DateTime1 added 3 days later, DateTime1 is still 2017/4/11, which has already been able to explain the problem.

2. To further determine the conclusion, the most reliable way is to look at the address of the variable, value type, dateTime1 and dateTime2 address should be different.

It does look different, and datetime is undoubtedly a value type.

Second, understand the datetime structure body

Go to the definition of datetime, and you can see that datetime is indeed a struct type,

It seems that the previous experiment is useless, but what is the structure of datetime, mainly the following members

The key to these members is ticks.

The time value of the ticks is in 100-nanosecond units, its 64 bits, the first two represent kind, and the next 62 bits represent the number of nanoseconds. The kind is used to indicate whether the DateTime structure represents local time, Coordinated Universal Time (UTC), or UTC and local time are unspecified. The Kind field is used to process conversions between local time and UTC time.

DateTime values range from 0001/1/1 00:00:00 to 9999/12/31 23:59:59

How are DateTime.Now and Datetime.utcnow calculated?

The anti-compilation DateTime.Now is as follows

 Public StaticDateTime now{[__dynamicallyinvokable]Get{DateTime UtcNow=Datetime.utcnow; BOOLISAMBIGUOUSDST =false; LongTicks = TIMEZONEINFO.GETDATETIMENOWUTCOFFSETFROMUTC (UtcNow, outisambiguousdst).        Ticks; Longnum = utcnow.ticks +ticks; if(Num >3155378975999999999L)        {            return NewDateTime (3155378975999999999L, datetimekind.local); }        if(Num <0L)        {            return NewDateTime (0L, datetimekind.local); }        return NewDateTime (num, datetimekind.local, ISAMBIGUOUSDST); }}

Too complicated to read, but you can see the general meaning is to get datetime.utcnow first, then convert to local time

Re-decompile Datetime.utcnow

 Public Static DateTime utcnow{    [__dynamicallyinvokable, securitysafecritical]    get    {         long systemtimeasfiletime = datetime.getsystemtimeasfiletime ();         return New DateTime ((ulong504911232000000000L4611686018427387904L);}    }

Finally, check Getsystemtimeasfiletime () is the Windows API, which is the function of getting the current UTC time

Whether DateTime in C # is a value type or a reference type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.