C# 协变 逆变

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace test
  7 {
  8     class Program
  9     {
 10         static void Main(string[] args)
 11         {
 12             Dog dog=new Dog();
 13             Animal animal=dog;
 14 
 15             Animal a1=new Animal();
 16             a1.Name="A1";
 17             Animal a2=new Animal();
 18             a2.Name="A2";
 19 
 20           
 21 
 22            //接口=OUT           
 23            // IEnumerable<Dog>
 24            // IEnumerator<Dog> 
 25            // IQueryable<Animal>
 26            // IGrouping<int,string>
 27 
 28             // 接口=IN
 29             //  IComparer<Dog>
 30             //  IComparable<Dog>
 31             //  IEqualityComparer<Dog>
 32 
 33             //  委托  IN | OUT       
 34             //  Action<int>
 35             //  Func<int,string>
 36             //  Converter<int,string>
 37             //  Comparison<int>
 38             //  Predicate<int>
 39 
 40 
 41             var listdog=GetDogs();
 42             IEnumerable<Animal> list1=listdog;
 43             foreach (var item in list1)
 44             {
 45                 item.Call();
 46             }
 47 
 48             List<Animal> list2 = listdog.Select(d => (Animal)d).ToList();
 49             foreach (var item in list2)
 50             {
 51                 item.Call();
 52             }
 53          
 54             
 55 
 56             //协变: 具体 赋给 抽象 
 57             IOut<Dog> myDogs = new TestOut<Dog>();
 58             IOut<Animal> myAnimals = myDogs;
 59             IOut<Animal> animals222=new TestOut<Dog>();
 60 
 61        
 62 
 63             //逆变: 抽象 赋给 具体 
 64             IIn<Animal> myAnimals2 = new TestIn<Animal>();            
 65             IIn<Dog> myDogs2 = myAnimals2;
 66             IIn<Dog> dogsss=new TestIn<Animal>();
 67 
 68 
 69             Console.ReadKey();
 70         }
 71 
 72         static IEnumerable<Animal> GetDogs()
 73         {
 74             Dog dog1=new Dog();
 75             dog1.Name="one";
 76             dog1.Age=11;
 77             Dog dog2=new Dog();
 78             dog2.Name="two";
 79             dog2.Age=22;
 80 
 81                DogEqual e=new DogEqual();
 82                e.Equals(dog1, dog2);
 83             
 84 
 85 
 86             List<Dog> listdog=new List<Dog>();
 87             listdog.Add(dog1);
 88             listdog.Add(dog2);
 89             return listdog;
 90         }
 91 
 92         static void ShowDog(IIn<Dog> dog)
 93         {
 94             
 95         }
 96 
 97         public interface IAnimal
 98         {
 99             void Call();
100         }
101         public  class Animal:IAnimal
102         {
103             public string Name;
104             public virtual void Call()
105             {
106                 Console.WriteLine("Animal");
107             }
108         }
109         public class Dog : Animal
110         {
111             public new string Name;
112             public int Age;
113             public override void Call()
114             {
115                 Console.WriteLine("Dog!");
116             }
117         }
118 
119         //out 协变  用于返回
120         public interface IOut<out T>
121         {
122             T GetElement();
123            
124         }
125 
126         public class TestOut<T> : IOut<T> where T:new()
127         {
128             public T GetElement()
129             {
130                 return new T();
131             }
132         }
133 
134         //in 逆变 用于传入
135         public interface IIn<in T>
136         {
137             void ChangeT(T t);
138         }
139         public class TestIn<T> : IIn<T> where  T:IAnimal
140         {
141             public void ChangeT(T t)
142             {
143                 t.Call();
144             }
145         }
146 
147 
148         public class animalEqual : IEqualityComparer<Animal>
149         {
150 
151             public bool Equals(Animal x, Animal y)
152             {
153 
154                 return x.Name.Equals(y.Name);
155             }
156 
157             public int GetHashCode(Animal obj)
158             {
159                 return 1;
160             }
161 
162         }
163 
164 
165 
166         public class DogEqual : IEqualityComparer<Dog>
167         {
168 
169             public bool Equals(Dog x, Dog y)
170             {
171 
172                 return x.Name.Equals(y.Name) && x.Age==y.Age;
173             }
174 
175             public int GetHashCode(Dog obj)
176             {
177                 return 1;
178             }
179 
180         }
181         
182     }
183 }

 逆变:http://msdn.microsoft.com/zh-cn/library/ms173174%28VS.80%29.aspx

原文地址:https://www.cnblogs.com/AspDotNetMVC/p/2822228.html