A simple example is to understand the covariant and inverter of C,

Source: Internet
Author: User

A simple example is to understand the covariant and inverter of C,

The interpretation of SolidMango is preferable for covariant inverters. For example, if you want to return the IEnumerable <object> type, you can use IEnmerable <string> instead. If you have an inverter, for example, in the IComparable <string> type parameter method, you can use the IComparable <object> type parameter instead.

Covariant

First, let's get started with covariant. There are two parent classes and child classes with inheritance relationships.

    public class Animal    {        public string Name { get; set; }    }    public class Dog : Animal    {        public Dog(string dogName)        {            Name = dogName;        }    }

Currently, the form parameter type of a help class method is the parent class set IEnumerable <Animal>.

    public class MyHelper    {        public void PrintAnimalNames(IEnumerable<Animal> animals)        {            foreach (var animal in animals)            {                Console.WriteLine(animal.Name);            }        }    }

With the covariant, You can input a real parameter of the IEnumerable <Dog> type in the PrintAnimalNames method to replace the IEnumerable <Animal> type.

Static void Main (string [] args) {List <Dog> dogs = new List <Dog> () {new Dog ("Puppy petty "), new Dog ("Puppy lily")}; // covariant IEnumerable <Animal> animals = dogs; MyHelper myHelper = new MyHelper (); myHelper. printAnimalNames (animals); Console. readKey ();}

It can be seen that the method can pass in the real parameters of the derived class interface type when calling this method based on the basic class interface type parameters.

 

Inverter

Let's try again. It is still two parent classes and child classes with inheritance relationships.

    public class Animal     {        public string Name { get; set; }        public int Age { get; set; }    }    public class Cat : Animal    {        public Cat(string catName, int catAge)        {            Name = catName;            Age = catAge;        }    }

Now we want to compare the two instances of the base class Animal. Therefore, it is necessary to write a class to implement the IComparer <Animal> interface.

    public class AnimalSizeComparator : IComparer<Animal>    {        public int Compare(Animal x, Animal y)        {            if (x != null && y != null)            {                if (x.Age > y.Age)                {                    return 1;                }                else if (x.Age == y.Age)                {                    return 0;                }                else                {                    return -1;                }            }            else            {                return -1;            }        }    }

Compare Cat in the methods in the help class. The method receives parameters of the IComparer <Cat> type.

Public class MyHelper {public void CompareCats (IComparer <Cat> catComparer) {var cat1 = new Cat ("kitten 1", 1); var cat2 = new Cat ("kitten 2 ", 2); if (catComparer. compare (cat2, cat1)> 0) {Console. writeLine ("kitten 2 wins");} else {Console. writeLine ("kitten 1 wins ");}}}

When the client calls the CompareCats method of MyHelper, real parameters of the IComparer <Animal> type can be passed in.

            IComparer<Animal> animalComparer = new AnimalSizeComparator();            MyHelper myHelper = new MyHelper();            myHelper.CompareCats(animalComparer);            Console.ReadKey(); 

It can be seen that the method can pass in the real parameters of the base class interface type when calling this method based on the form parameters of the derived class interface type.

Conclusion: In this scenario, the derived class interface replaces the parent class interface, which is called a covariant; the parent class interface replaces the derived class interface, which is called an inverter.

The above motion graphs are provided by "Graph dado ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.