加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 03:48:05 所属栏目:百科 来源:网络整理
导读:是否可以在C#泛型方法中返回对象类型或Nullable类型? 例如,如果我有一个List的安全索引访问器,并且我想返回一个值,以后我可以使用== null或.HasValue()检查. 我目前有以下两种方法: static T? SafeGetT(ListT list,int index) where T : struct { if (list
是否可以在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]);
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读