using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp4 { //>>Allowing you do things in your code that previously//you where surprised you could not do. //-- Anders Hejlsberg //>>covariance and contravariance are introduce in .Net 4.0 class Animal { public void Walk() { Console.WriteLine("Animal can walk"); } } class Dog : Animal { public void Walk() { Console.WriteLine("Dog can walk"); } } class Cat : Animal { public void Walk() { Console.WriteLine("Cat can walk"); } } class Program { public static void Main(string[] args) { Animal obj = new Cat(); obj = new Cat(); //>>Covariance preserves assignment compatbility//between parent and child // relationship during dynamic plymorphism //>>After .Net 4.0 you can also assign group of//objects from child to parents like below IEnumerable<Animal> animals = new List<Dog>(); animals = new List<Cat>(); Action<Dog> contravarient = DescribeAnimal; contravarient(new Dog()); Action<Dog> contravarient2 = new Action<Animal>((Animal animal) => { }); Console.ReadKey(); } static void DescribeAnimal(Animal animal) { Console.WriteLine(animal.GetType().Name); } } }
Saturday, August 4, 2018
What is covariance and contravariance in c# ?
Subscribe to:
Post Comments (Atom)
Find the value from array when age is more than 30
const data = [ { id: 1, name: 'Alice', age: 25 }, { id: 2, name: 'Bob', age: 30 }, { id: 3, name: 'Charlie', ...
-
-- ============================================= --...
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name=...
-
int[] anArray = { 2, 5, 10, 9, 10, 6 }; int? maxVal = null; //nullable so this works even if you have all super-lo...
No comments:
Post a Comment