Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can DateTimeFormatInfo.CurrentInfo be null

I have the following code in my C# application.

DateTimeFormatInfo.CurrentInfo.DayNames

ReSharper 7.1.1 is highlighting the fact that the DateTimeFormatInfo.CurrentInfo could cause a null reference exception.

Under what circumstances would this occur? Or is this just a mistake on ReSharper's part believing that any object whose property you access should be null checked?

like image 749
Ryan Gates Avatar asked Jun 04 '13 18:06

Ryan Gates


1 Answers

ReSharper is most probably just doing lexical analysis here and nothing deeper.

Since DateTimeFormatInfo is a class, a variable of this type can be null. Which means that the instance returned by DateTimeFormatInfo.CurrentInfo can be a null reference.

That's the error you are getting.

ReSharper doesn't understand that the method was coded such that it will not return a null reference, so it gives a warning.

Don't take the messages from ReSharper as scripture...

like image 100
Oded Avatar answered Oct 16 '22 16:10

Oded