Invariance, covariance and contravariance

来源:互联网 发布:卡西.阿弗莱克 知乎 编辑:程序博客网 时间:2024/05/02 18:01
            List<string> stringList = new List<string>();            //List<object> objectList = stringList; // No. Invariance            Parent p = new Parent();            Test(ref p);            Child c = new Child();            //Test(ref c); // No.            Parent pc = new Child();            Test(ref pc);            Parent pc2 = c;            Test(ref pc2);            List<string> stringList2 = new List<string>();            IEnumerable<object> objects = stringList2;  // C# 4.0 OK covariance            /* covariance out only             public interface IEnumerable<out T> : IEnumerable             * {             * IEnumerator<T> GetEnumerator();             * }             */            /* contravariance ref ICompareable, IN*/
<pre name="code" class="csharp">        public void Test(ref Parent p)        { }        public class Parent        { }        public class Child : Parent        { }


留给自已记忆用的~                                             
0 0
原创粉丝点击