【发布时间】:2022-12-17 03:07:55
【问题描述】:
为什么我在 list2 的行中收到警告?我在这里过滤掉了所有的空值。警告指出,在 select 方法中可能会取消引用空值。
#nullable enable
using System.Collections.Generic;
using System.Linq;
namespace Secunet.Bocoa.AtBmi.SecondLineCheckPlugin
{
public class Test
{
public Test()
{
List<string?> testList = new List<string?>()
{
"hallo",
null
};
IEnumerable<string> list2 = testList.Where(x => x != null).Select(x => x.Replace("A", "")); // warning
IEnumerable<string> list3 = testList.Where(x => x != null).Select(x => x != null ? x.Replace("A", "") : ""); // no warning
}
}
}
在 list3 的行中没有发出警告,但是 Select-Statement 中的检查总是没有意义的。
【问题讨论】:
标签: c# linq nullable-reference-types