If DateTime is immutable, why does the following work?

前端 未结 6 1488
轻奢々
轻奢々 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条回答
  •  旧时难觅i
    2021-01-03 21:31

    See this.

    Read the description of all those methods. It always says "Returns a new DateTime...". It does not change the current DateTime object, thus it is immutable.

    Variable reference is a different thing. Think of it just as a pointer to the actual immutable DateTime object that can be changed to point a different one.

提交回复
热议问题