具有不可为null的泛型参数的C#泛型方法警告可能的null引用

具有不可为null的泛型参数的C#泛型方法警告可能的null引用,c#,generics,nullable,nullable-reference-types,C#,Generics,Nullable,Nullable Reference Types,今天编程时出现了以下情况。 泛型方法接受不可为null的泛型参数。该方法基本上只是将泛型值插入到集合中。但是,这会引起编译器警告,指示泛型参数可能为null。从表面上看,这似乎是编译器中的一个bug,与可空值的设计相矛盾。然而,我相信有一些很好的解释,我没有看到 考虑以下情况的简化示例: publicsvoidm1(T,列表l)=>l.Add(T) 编译器警告t在l.Add(t)中可能为null 此外,为了确保完整性,以下方法给出了相同的错误(如预期的那样): publicsvoidm1(T?T

今天编程时出现了以下情况。 泛型方法接受不可为null的泛型参数。该方法基本上只是将泛型值插入到集合中。但是,这会引起编译器警告,指示泛型参数可能为null。从表面上看,这似乎是编译器中的一个bug,与可空值的设计相矛盾。然而,我相信有一些很好的解释,我没有看到

考虑以下情况的简化示例:
publicsvoidm1(T,列表l)=>l.Add(T)
编译器警告t在
l.Add(t)
中可能为null

此外,为了确保完整性,以下方法给出了相同的错误(如预期的那样):
publicsvoidm1(T?T,列表l)=>l.Add(T)

有人对此有很好的见解吗?

有人可以打电话:

var list = new List<object>();
M1<string?>(null, list);
这会给你一个警告:

M1<string?>(null, new List<object>());
^^^^^^^^^^^
// warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic 
// type or method 'C.M1<T>(T, List<object>)'. Nullability of type argument 'string?' doesn't
// match 'notnull' constraint.
M1<string?>(null, new List<object>());
            ^^^^
// warning CS8625: Cannot convert null literal to non-nullable reference type.
这会给你一个警告:

M1<string?>(null, new List<object>());
^^^^^^^^^^^
// warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic 
// type or method 'C.M1<T>(T, List<object>)'. Nullability of type argument 'string?' doesn't
// match 'notnull' constraint.
M1<string?>(null, new List<object>());
            ^^^^
// warning CS8625: Cannot convert null literal to non-nullable reference type.
M1(空,新列表());
^^^^
//警告CS8625:无法将null文本转换为不可为null的引用类型。
当然,如果您想允许
null
,那么您的
l
需要是一个
列表

有人可以呼叫:

var list = new List<object>();
M1<string?>(null, list);
这会给你一个警告:

M1<string?>(null, new List<object>());
^^^^^^^^^^^
// warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic 
// type or method 'C.M1<T>(T, List<object>)'. Nullability of type argument 'string?' doesn't
// match 'notnull' constraint.
M1<string?>(null, new List<object>());
            ^^^^
// warning CS8625: Cannot convert null literal to non-nullable reference type.
这会给你一个警告:

M1<string?>(null, new List<object>());
^^^^^^^^^^^
// warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic 
// type or method 'C.M1<T>(T, List<object>)'. Nullability of type argument 'string?' doesn't
// match 'notnull' constraint.
M1<string?>(null, new List<object>());
            ^^^^
// warning CS8625: Cannot convert null literal to non-nullable reference type.
M1(空,新列表());
^^^^
//警告CS8625:无法将null文本转换为不可为null的引用类型。
当然,如果您想允许
null
,那么您的
l
需要是一个
列表

有人可以呼叫:

var list = new List<object>();
M1<string?>(null, list);
这会给你一个警告:

M1<string?>(null, new List<object>());
^^^^^^^^^^^
// warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic 
// type or method 'C.M1<T>(T, List<object>)'. Nullability of type argument 'string?' doesn't
// match 'notnull' constraint.
M1<string?>(null, new List<object>());
            ^^^^
// warning CS8625: Cannot convert null literal to non-nullable reference type.
这会给你一个警告:

M1<string?>(null, new List<object>());
^^^^^^^^^^^
// warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic 
// type or method 'C.M1<T>(T, List<object>)'. Nullability of type argument 'string?' doesn't
// match 'notnull' constraint.
M1<string?>(null, new List<object>());
            ^^^^
// warning CS8625: Cannot convert null literal to non-nullable reference type.
M1(空,新列表());
^^^^
//警告CS8625:无法将null文本转换为不可为null的引用类型。
当然,如果您想允许
null
,那么您的
l
需要是一个
列表