public MethodCalling() { Console.WriteLine(Environment.NewLine + "Method Calling: "); dynamic t1 = new Dog(); ttest(t1); dynamic t2 = new BossDog(); ttest(t2); dynamic t3 = new object(); ttest(t3); dynamic t4 = "test"; ttest(t4); IAnimal a = new Dog(); dynamic t5 = a; ttest(t5); }
public Variance() { Console.WriteLine(Environment.NewLine + "Variance:"); // Array does not use the element itself, it only returns it, therefor it can be covariance // An method that implement an interface, uses the element, therefor it cant be covariance. Animal cant act like a dog. Animal[] a = new Dog[10]; //array of dogs, cast to animals. its still dogs ;) crashingMethod(a); a[0] = new Dog(); a[1] = new BossDog(); //Ok, cuz BossDogs is dogs //a[0] = new Cat(); crash, cuz Cat can act like a Dog. Compiler don't notice this.. //a[1] = new Animal(); Animal k = a[0]; Console.WriteLine(k); }
public LateBinding() { IAnimal[] arr = new Dog[3]; //array of dogs, cast to animals. its still dogs ;) arr[0] = new Dog(); arr[1] = new BossDog(); //Ok, cuz BossDogs is dogs Console.WriteLine("Early binding: (Normal)"); foreach (var a in arr) { ttest(a); } // type is determine at runtime // compiler does not help with dynamic Console.WriteLine("Late binding: (dynamic)"); foreach (dynamic a in arr) { ttest(a); } }
void ttest(BossDog a) => Console.WriteLine("\tBossDog");