If DateTime is immutable, why does the following work?

前端 未结 6 1500
轻奢々
轻奢々 2021-01-03 20:34

I thought I understood what Immutable meant, however I don\'t understand why the following compiles and works:

DateTime dt = DateTime.Now;

Console.WriteLine         


        
6条回答
  •  醉酒成梦
    2021-01-03 21:36

    The Now property is something like:

     DateTime Now {
         get {
             // Get the OS time
             return new DateTime(year, month, day, hour, min, sec...)
         }
     }
    

    (technically false, the Now calls internally the UtcNow that calls the OS :-), but you get the idea).

    The DateTime.Now is a factory for DateTime :-)

提交回复
热议问题