Why is DateTime.Now a property and not a method?

后端 未结 6 1288
眼角桃花
眼角桃花 2020-12-08 13:06

After reading this blog entry : http://wekeroad.com/post/4069048840/when-should-a-method-be-a-property,

I\'m wondering why Microsoft choose in C# :

D         


        
6条回答
  •  旧时难觅i
    2020-12-08 13:37

    It actually is deterministic; it's output is not random, but is based on something quite predictable.

    The 'current time' changes all the time; so to be relatively "the same" with each call, that value must change so that every time it's called, it's returning the current time.

    EDIT:

    This just occurred to me: Of course, two subsequent calls to a property getter can return different results, if something changed the property value in the interim. Properties are not supposed to be Constants.

    So, that's what happening (conceptually) with DateTime.Now; its value is being changed between subsequent calls to it.

提交回复
热议问题