DateTimeFormatInfo.CurrentInfo如何为null

Rya*_*tes 6 c# resharper nullreferenceexception datetimeformatinfo resharper-7.1

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

DateTimeFormatInfo.CurrentInfo.DayNames
Run Code Online (Sandbox Code Playgroud)

ReSharper 7.1.1强调了DateTimeFormatInfo.CurrentInfo可能导致空引用异常的事实.

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

Ode*_*ded 9

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

既然DateTimeFormatInfo是一个类,这个类型的变量可以null.这意味着返回的实例DateTimeFormatInfo.CurrentInfo 可以作为null参考.

这就是你得到的错误.

Resharper不理解该方法是编码的null,因此它不会返回引用,因此它会发出警告.

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