[英]How to make Generic parameter T Nullable in C#?
I'm creating a generic method which should be able to return null
. 我正在创建一个应该能够返回null
的泛型方法。
As an example I've created this method GetSecondSmallest(List<T>)
below. 作为一个例子,我在下面创建了这个方法GetSecondSmallest(List<T>)
。 This function loops through all of the IComparable
items and returns the second smallest item if possible. 此函数循环遍历所有IComparable
项,并在可能的情况下返回第二个最小项。 If this item does not exist it returns null
. 如果此项不存在,则返回null
。
public T? GetSecondSmallest<T>(List<T> items) where T : Nullable, IComparable
{
if (items.Count == 0) return null;
T smallest = items[0];
T? secondSmallest = null;
// Find second smallest by looping through list
return secondSmallest;
}
Two example usages of this method would be: 这种方法的两个示例用法是:
GetSecondSmallest(new List<int> {4, 2, 3, 1, 6, 8}) // Should return 2
GetSecondSmallest(new List<MyComparable> {new MyComparable('x')}) // Should return null
When you try to compile this code you'll see what the problem is here: 当您尝试编译此代码时,您将看到问题所在:
Error CS0717 'Nullable': static classes cannot be used as constraints 错误CS0717'Nullable ':静态类不能用作约束
How can I make this method return either an instance of T
or null
? 如何使此方法返回T
的实例或null
?
EDIT : I have already seen this question , but this does not answer my question, because in my situation the parameter is also generic, and should be Nullable
. 编辑 :我已经看到了这个问题 ,但这并没有回答我的问题,因为在我的情况下,参数也是通用的,应该是Nullable
。 So the provided answers there aren't applicable. 所以提供的答案不适用。
You can't narrow T
down as nullable or an object. 你不能将T
缩小为可空或对象。 You have to make two methods, one for T
and one for T?
你必须制作两种方法,一种用于T
,一种用于T?
. 。
public T? GetSecondSmallestAsNullable<T>(List<T> items) where T : struct
{
return null;
}
public T GetSecondSmallestAsClass<T>(List<T> items) where T : class
{
return null;
}
Use a wrapper class such as Tuple to wrap the resulting value. 使用包装类(如Tuple)来包装结果值。 In the example below, null is returned if there is no second smallest item, otherwise a Tuple is returned with the second smallest value. 在下面的示例中,如果没有第二个最小项,则返回null,否则返回具有第二个最小值的Tuple。
public static void Main()
{
var result1 = GetSecondSmallest(new List<int>
{
4, 2, 3, 1, 6, 8
}); // Should return 2
var result2 = GetSecondSmallest(new List<MyComparable>
{
new MyComparable('x')
}); // Should return null
Console.WriteLine("result1=" + result1.Item1);
Console.WriteLine("result2 == null: " + (result2 == null));
}
public static Tuple<T> GetSecondSmallest<T>(List<T> items)where T : IComparable
{
return items.Skip(1).Select(v => Tuple.Create(v)).FirstOrDefault();
}
Change the return type to Nullable, and call the method with the non nullable parameter 将返回类型更改为Nullable,并使用非可空参数调用该方法
Simple example: 简单的例子:
static void Main(string[] args)
{
int? i = GetValueOrNull<int>(null, string.Empty);
}
public static Nullable<T> GetValueOrNull<T>(DbDataRecord reader, string columnName) where T : struct
{
object columnValue = reader[columnName];
if (!(columnValue is DBNull))
return (T)columnValue;
return null;
}
Nullable type as a generic parameter possible? Nullable类型作为通用参数可能吗?
UPD UPD
Like this: 像这样:
public Nullable<T> GetSecondSmallest<T>(List<T> items) where T : IComparable
{
if (items.Count == 0) return null;
Nullable<T> smallest = items[0];
Nullable<T> secondSmallest = null;
// Find second smallest by looping through list
return secondSmallest;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.