为什么DateTime.Now属性,而不是一个方法? [英] Why is DateTime.Now a property and not a method?

查看:129
本文介绍了为什么DateTime.Now属性,而不是一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读这篇博客文章后:<一href="http://wekeroad.com/post/4069048840/when-should-a-method-be-a-property">http://wekeroad.com/post/4069048840/when-should-a-method-be-a-property,

我不知道为什么微软在C#中进行选择:

  DateTime的ADT = DateTime.Now;
 

而不是

  DateTime的ADT = DateTime.Now();
 

  • 的最佳做法说:连续两次调用该成员时,使用的方法会产生不同的结果
  • DateTime.Now 就是非determistic方法/属性很好的例子。

你知道,如果有任何原因的设计?
或者,如果它只是一个小错误?

解决方案

我相信通过C#CLR,杰弗里里希特提到DateTime.Now是一个错误。

  

本的System.DateTime类有一个只读   现在属性,返回当前的日期和时间。每次查询这个时候   属性,它会返回一个不同的值。这是一个错误,而微软祝愿   他们可以通过现在的方法,而不是一个属性的修复类。

通过C#第3版

CLR - 第243

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# :

DateTime aDt = DateTime.Now;

instead of

DateTime aDt = DateTime.Now();

  • Best practices say : Use a method when calling the member twice in succession produces different results
  • And DateTime.Now is perfect example of non-determistic method/property.

Do you know if there any reason for that design ?
Or if it's just a small mistake ?

解决方案

I believe in CLR via C#, Jeffrey Richter mentions that DateTime.Now is a mistake.

The System.DateTime class has a readonly Now property that returns the current date and time. Each time you query this property, it will return a different value. This is a mistake, and Microsoft wishes that they could fix the class by making Now a method instead of a property.

CLR via C# 3rd Edition - Page 243

这篇关于为什么DateTime.Now属性,而不是一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆