简体   繁体   English

操作数从不为空警告

[英]Operand Never Null Warning

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. 我正在编写一些逻辑,如果该逻辑不为null或将获得最小值,则该逻辑将获取对象的开始日期。

var statusStartDate = ((DateTime?)previousCategory?.StartDate) ?? DateTime.MinValue;

Resharper is generating a warning that the Operand is never null: Resharper正在生成一个警告,即操作数永远不会为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? 如果场景中的上一个类别为nu​​ll,那么这将如何不创建为null的左操作数,或者这仅仅是reshaper中的错误?

EDIT 编辑

Verifyable Example with DateTime 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: 如果运行测试对象:

public class Test
{
    public DateTime startDate;
}

void main()
{
 var t = new test();
var time=t.startDate
}

time varible is not null and has "{1/1/0001 12:00:00 }" value it's the reason that resharper make a warning. time变量不为null且具有“ {1/1/0001 12:00:00}”值,这是reshaper发出警告的原因。 In some situation for exmaple FirstOrDefault(x=>x...) on a list, that may have null result, you can user youe code Without any warning 在某些情况下,例如列表上的FirstOrDefault(x => x ...),结果可能为空,您可以使用您的代码而无需任何警告

在此处输入图片说明

Update:In second senario Resharper makes mistake because: 更新:在第二个参议员里沙珀犯错是因为:

var check = ((DateTime?) tmp?.startDate).HasValue;// false , startDate is null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM