I'm writing some logic which will get a start date on an object if it isn't null or it will get the min value.
var statusStartDate = ((DateTime?)previousCategory?.StartDate) ?? DateTime.MinValue;
Resharper is generating a warning that the Operand is never null:
If the previous category in the scenario is null, how would this not create a left hand operand being null or is this just a bug in resharper?
EDIT
Verifyable Example with DateTime
public class Test
{
public Date2 startDate;
public List<Test> testList = new List<Test>();
public struct Date2
{
public static explicit operator Date2(DateTime date)
{
return default(Date2);
}
public static explicit operator DateTime(Date2 date)
{
return default(DateTime);
}
}
public DateTime BS()
{
var tmp = testList.LastOrDefault();
var testDate = ((DateTime?)tmp?.startDate) ?? DateTime.MaxValue;
return testDate;
}
}
var test = new Test();
test.BS();
var x = test.BS();
If you run your test Object:
time varible is not null and has "{1/1/0001 12:00:00 }" value it's the reason that resharper make a warning. In some situation for exmaple FirstOrDefault(x=>x...) on a list, that may have null result, you can user youe code Without any warning
Update:In second senario Resharper makes mistake because: