我有一个通用的方法与这个(dummy)代码(是的,我知道IList有谓词,但我的代码不是使用IList而是一些其他的集合,无论如何这是无关紧要的问题…)

    static T FindThing<T>(IList collection, int id) where T : IThing, new()
    {
        foreach (T thing in collection)
        {
            if (thing.Id == id)
                return thing;
        }
        return null;  // ERROR: Cannot convert null to type parameter 'T' because it could be a value type. Consider using 'default(T)' instead.
    }

这给了我一个构建错误

"不能将null转换为类型参数 因为它可以是一个值类型。 考虑使用‘default(T)’。”

我能避免这个错误吗?


当前回答

下面是您可以使用的两个选项

return default(T);

or

where T : class, IThing
 return null;

其他回答

如果你有对象,那么需要类型转换 返回(T)(对象)(员工); 如果需要返回null。 返回默认值(T);

解决方案的软件绝地作品,

你也可以使用一对value和nullable类型来存档:

static T? FindThing<T>(IList collection, int id) where T : struct, IThing
{
    foreach T thing in collecion
    {
        if (thing.Id == id)
            return thing;
    }
    return null;
}

因为IThing是接口不可能使用null。因此,必须使用default(T)来确定实际类型T的默认值,该类型T是在调用函数之前定义的。

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        
        IThing x = new List<Thing>().FindThing(1);
        
    }

}

public static class Ext {
    public static T FindThing<T>(this IList<T> collection, int id) where T : IThing, new()
    {
        foreach (T thing in collection)
        {
            if (thing.Id == id) return (T)thing;
        }
    
        //return null; //not work
        //return (T)null; //not work
        //return null as T; //not work
        return default(T); //work
    }
}

public interface IThing { int Id {get; set;} }
public class Thing : IThing { public int Id {get;set;}}

上述2个答案的另一种选择。如果将返回类型更改为object,则可以返回null,同时转换非null返回。

static object FindThing<T>(IList collection, int id)
{
    foreach T thing in collecion
    {
        if (thing.Id == id)
            return (T) thing;
    }
    return null;  // allowed now
}
return default(T);