简体   繁体   中英

C#: How can DateTime.Now be just an attribute?

I'm trying to understand how the implementation of the Now attribute in DateTime works. My background is mainly Python and Haskell so I can't by my life understand how the Now attribute can "return" different values depending on when you use it.

My intuition says that Now should really be a function which does some low-level magic followed by some high-level magic and then returns a DateTime object with the correct time.

DateTime.Now isn't an attribute, it's a static readonly property.

Under the covers a readonly property is just a function call that returns a value, so it can do any amount of processing it wants to.

Hope this helps.

It's not an attribute, it's a static property on the DateTime class that looks like the following:

public static DateTime Now
{
    get
    {
        return UtcNow.ToLocalTime();
    }
}

UtcNow is another property on DateTime that returns the following:

return new DateTime((ulong) ((GetSystemTimeAsFileTime() + 
  0x701ce1722770000L) | 0x4000000000000000L));

GetSystemTimeAsFileTime is a Windows API Call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM