我试图在这一行代码中使用新的C# 7模式匹配功能。
if (Customers.SingleOrDefault(x => x.Type == CustomerType.Company) is Customer customer)
{
...
}
但出于某种原因,Resharper/VisualStudio2017在is Customer
下给我一个警告,并给出以下消息
源表达式总是模式的类型,匹配所有非空值。
但是customer
也可以是null
,对吗?有人能给我解释一下为什么会发出这样的警告吗?
发布于 2017-07-31 22:31:57
你是对的!
ReSharper (不是Visual )实际上是正确的,尽管我不知道为什么会是警告。
虽然Customers
是Customer
的集合,但是SingleOrDefault
的使用暗示值可能是null
,而不是Customer
。
没有什么能说明Customers
产生的所有值都是非null
的。
https://stackoverflow.com/questions/45429913
复制相似问题