DateTimeFormatInfo.CurrentInfo如何为null

时间:2013-06-04 18:02:50

标签: c# resharper nullreferenceexception resharper-7.1 datetimeformatinfo

我在C#应用程序中有以下代码。

DateTimeFormatInfo.CurrentInfo.DayNames

ReSharper 7.1.1突出显示DateTimeFormatInfo.CurrentInfo可能导致空引用异常的事实。

在什么情况下会发生这种情况?或者这只是ReSharper的一个错误,认为你访问的属性的任何对象应该被空检查?

1 个答案:

答案 0 :(得分:9)

ReSharper很可能只是在这里进行词法分析,而不是更深入。

由于DateTimeFormatInfo是一个类,因此的此类变量可以null。这意味着DateTimeFormatInfo.CurrentInfo 返回的实例可以是null引用。

这是你得到的错误。

Resharper不理解该方法已被编码,因此它不会返回null引用,因此会发出警告。

不要将Resharper的消息作为经文......

相关问题