static void Main() { B[] data1 = new B[] { new B(), new B(), new B() }; B[] data2 = new B[] { new C(), new B(), new C() }; C[] data3 = new C[] { new C(), new C(), new C() }; List <B> list1 = new List <B>(data1); List <C> list3 = new List <C>(data3); ArraysVariance.ShowBs(data1); ArraysVariance.ShowBs(data2); ArraysVariance.ShowBs(data3); ArraysVariance.ReplaceB(data1, new B()); ArraysVariance.ReplaceB(data1, new C()); ArraysVariance.ReplaceB(data3, new C()); try { ArraysVariance.ReplaceB(data3, new B()); } catch (Exception e) { Console.WriteLine(e); } GenericsInvariance.ShowListOfBs(list1); //GenericsInvariance.ShowListOfBs(list3); DelegatesVariance.CreateAndShow(DelegatesVariance.makeB); DelegatesVariance.CreateAndShow(DelegatesVariance.makeC); DelegatesVariance.CallUseB(DelegatesVariance.PrintA, new C()); DelegatesVariance.CallUseB(DelegatesVariance.PrintB, new C()); //DelegatesVariance.CallUseB(DelegatesVariance.PrintC, new B()); }
static void Main() { B[] data1 = new B[] { new B(), new B(), new B() }; B[] data2 = new B[] { new C(), new B(), new C() }; C[] data3 = new C[] { new C(), new C(), new C() }; List <B> list1 = new List <B>(data1); // List<B> list2 = new List<C>(data3); // List<C> is not a List<B> List <C> list3 = new List <C>(data3); ArraysVariance.ShowBs(data1); ArraysVariance.ShowBs(data2); ArraysVariance.ShowBs(data3); // C[] is a B[] (for reading) Console.WriteLine(); ArraysVariance.ReplaceB(data1, new B()); ArraysVariance.ReplaceB(data1, new C()); ArraysVariance.ReplaceB(data3, new C()); try { ArraysVariance.ReplaceB(data3, new B()); // C[] is not a B[] for writing } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine(); GenericsInvariance.ShowListOfBs(list1); // GenericsInvariance.ShowListOfBs(list3); // List<C> is not a List<B> Console.WriteLine(); // DelegatesVariance.CreateAndShow(DelegatesVariance.makeA); DelegatesVariance.CreateAndShow(DelegatesVariance.makeB); DelegatesVariance.CreateAndShow(DelegatesVariance.makeC); Console.WriteLine(); DelegatesVariance.CallConsumeB(DelegatesVariance.PrintA, new C()); DelegatesVariance.CallConsumeB(DelegatesVariance.PrintB, new C()); //DelegatesVariance.CallConsumeB(DelegatesVariance.PrintC, new B()); }