是否可以在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]); }
原文地址:https://www.jb51.cc/csharp/93672.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。