在单个C#泛型方法中返回nullable和null?

是否可以在C#泛型方法中返回对象类型或Nullable类型?

例如,如果我有一个List的安全索引访问器,并且我想返回一个值,以后我可以使用== null或.HasValue()检查.

我目前有以下两种方法

static T? SafeGet<T>(List<T> list,int index) where T : struct 
{
    if (list == null || index < 0 || index >= list.Count)
    {
        return null;
    }

    return list[index];
}

static T SafeGetobj<T>(List<T> list,int index) where T : class
{
    if (list == null || index < 0 || index >= list.Count)
    {
        return null;
    }

    return list[index];
}

如果我尝试将方法组合成一个单一的方法.

static T SafeGetTest<T>(List<T> list,int index)
{
    if (list == null || index < 0 || index >= list.Count)
    {
        return null;
    }

    return list[index];
}

我得到一个编译错误

Cannot convert null to type parameter ‘T’ because it Could be a non-nullable value type. Consider using ‘default(T)’ instead.

但是我不想使用default(T),因为在原语的情况下,0是int的认值,是一个可能的实际值,我需要区分不可用的值.

这些方法可以组合成单一方法吗?

(为了纪录我使用的是.NET 3.0,而我对更现代的C#可以做的事情感兴趣,我个人只能使用3.0中的答案)

解决方法

不是正是你想要的,但可能的解决方法是返回一个元组(或其他包装类):
static Tuple<T> SafeGetobj<T>(List<T> list,int index) 
    {
        if (list == null  || index < 0 || index >= list.Count)
        {
            return null;
        }

        return Tuple.Create(list[index]);
    }

Null总是意味着不能获得任何值,单个元组本身将意味着一个值(即使值本身可以为null).

在vs2015你可以使用?调用时的符号:var val = SafeGetobj(somedoublelist,0)?Item1;
当然,您可以创建自己的通用包装器,而不是元组.

如上所述,不完全是最佳的,但它将是一个可行的工作,并且具有能够看到不是有效选择和零元素之间的差异的附加好处.

自定义包装器实现的示例:

struct IndexValue<T>
    {
        T value;
        public bool Succes;
        public T Value
        {
            get
            {
                if (Succes) return value;
                throw new Exception("Value Could not be obtained");
            }
        }

        public IndexValue(T Value)
        {
            Succes = true;
            value = Value;
        }

        public static implicit operator T(IndexValue<T> v) { return v.Value; }
    }

    static IndexValue<T> SafeGetobj<T>(List<T> list,int index) 
    {
        if (list == null || index < 0 || index >= list.Count)
        {
            return new IndexValue<T>();
        }

        return new IndexValue<T>(list[index]);
    }

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...