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

后端 未结 6 1287
眼角桃花
眼角桃花 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:49

    According to MSDN you should use a property when something is a logical data member of the object:

    http://msdn.microsoft.com/en-us/library/bzwdh01d%28VS.71%29.aspx#cpconpropertyusageguidelinesanchor1

    The go on to list out the cases where a method would be more appropriate. What is ironic is that one of the rules for a method is to use it when successive calls may return different results and of course Now certainly meets that criteria.

    Personally I think this was done to eliminate the needs for the extra (), but I have found the absence of () confusing; it took me a little while to shift from the old approach in VB/VBA.

提交回复
热议问题