Why IEnumerable<T> is defined as IEnumerable<out T>, not IEnumerable<T>
2013-05-17 10:40
267 查看
http://stackoverflow.com/questions/12424537/why-ienumerablet-is-defined-as-ienumerableout-t-not-ienumerablet
The
In practice,
If I define two interfaces.
Then, I implement them like this.
Then I try to compile this code,
The is because the covariant interface allows more derived instances, where as, the standard interface does not.
The
outtype parameter specifier denotes covariance.
In practice,
If I define two interfaces.
[code]ISomeInterface<T>{} ISomeCovariantInterface<out T>{}[/code]
Then, I implement them like this.
[code]SomeClass<T>:ISomeInterface<T>,ISomeCovariantInterface<T>{}[/code]
Then I try to compile this code,
[code]ISomeCovariantInterface<object>=newSomeClass<string>();// works ISomeInterface<object>=newSomeClass<string>();// fails[/code]
The is because the covariant interface allows more derived instances, where as, the standard interface does not.
public interface ISomeInterface<T> { } public interface ISomeCovariantInterface<out T> { } public class SomeClass<T> : ISomeInterface<T>, ISomeCovariantInterface<T> { } var obj = new SomeClass<string>(); ISomeCovariantInterface<object> obj1 = obj;// works ISomeCovariantInterface<string> obj2 = obj;// works ISomeInterface<string> obj3 = obj;//work //ISomeInterface<object> obj4 = obj;//不可编译
相关文章推荐
- <c:choose> is not allowed as a child of,Only <c:when> and <c:otherwise> are allowed children.
- Python使用eval强制转换字符串为字典时报错:File "<string>", line 1, in <module> NameError: name 'nan' is not defined
- fatal error C1853: '<filename>' is not a precompiled header file
- jsp页面使用表达式<%=basePath%>跳转报“ attribute for %> is not properly terminated”错误
- 问题解决[<UIViewController 0x8430220>setValue:forUndefinedKey:]this class is not key value..
- Device Checks for ASM Fails with PRVF-5150: Path ORCL:<diskname> is not a valid path [ID 1210863.1]
- How to: Implement CopyToDataTable<T> Where the Generic Type T Is Not a DataRow
- '<>' operator is not allowed for source level below 1.7
- The MagicAjax HttpModule is not included in web.config. Add [<httpModules><add name="MagicAjax" type="MagicAjax.MagicAja
- PCL 中 pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr 出现incomplete type is not allowed 的解决方法
- iOS - 报错 Warning: Attempt to present <xx: xx> on <xx: xx> whose view is not in the window hierarchy!
- java.lang.IllegalAccessException: void #####.MyBroadcastReceiver.<init>() is not accessible from jav
- Illegal redeclaration of module <glbl>.&Element index 2 into memp is out of bounds
- - Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>, as they will not be parsed.
- The type List is not generic; it cannot be parameterized with arguments <Integer>
- eclipse报错:'<>' operator is not allowed for source level below 1.7
- '<>' operator is not allowed for source level below 1.7
- Property 'submit' of object #<HTMLFormElement> is not a function
- Illegal redeclaration of module <glbl>.&Element index 2 into memp is out of bounds
- 判断 sql 中的空字符串'' , 需要指定两点:is NOT NULL 和 <>‘’