How to understand the inverter and covariant?

And inverter covariant

  • The so-called inverter and covariance, .net is object-oriented in order to polymorphic conversion characteristics Richter moved generics can still be used.

Covariance:

  1. Existing the following categories:

     public class Person
    {
        public long Id { get; set; }
        public string Name { get; set; }
    }
    public class Chinese:Person
    {
    ​
    }
    ​
    ​
    ​
    public interface XieBian<T>
    {
        T Print();
    }
    public class XieBianInstance<T> : XieBian<T>
    {
        public T Print()
        {
            throw new NotImplementedException();
        }
    }

     

  2. According to the object-oriented multi-state characteristics, the parent can point to a variable of type Object sub-class type, then the following code in accordance with the terms of the theory should be no problem, but the compiler error: " Can not type Chinese converted to the Person ." The original Generics are not supported.

    XieBian<Person> xie = new XieBianInstance<Chinese>();

    So if you want to use, it would have to use covariant, in fact, on a generic interface or delegate plus the out keyword

    public interface XieBian<out T>
    {
        T Print();
    }
    public class XieBianInstance<T> : XieBian<T>
    {
        public T Print()
        {
            throw new NotImplementedException();
        }
    }

  Note: covariant generic type can act on the interface or delegate, and the generic type can only be used covariant interface method return value or delegate

Inverter:

  1. An existing delegate:

    public delegate void TestDel<T>(T obj);
  2. At first glance it looks like a little common sense and not the sub-class type variable to point to the parent class type object. If you change a way of thinking, this commission is to pass an object into a type of Chinese, because Chinese is inherited Person, according to the Richter conversion available, so write no problem. The problem is being given: " Can not Type Person converted into Chinese ." Generics are not supported to do so too.

    TestDel<Chinese> testDel = new TestDel<Person>((ch)=> { }); 

    If you want to do, you have to use an inverter, in fact, it was commissioned in the above plus the generic keyword int

    public delegate void TestDel<in T>(T obj);

    Into this, the above code can be properly executed.

  Note: Inverse become generic type can only act on the interfaces and delegates, and the inverse becomes only a generic type parameter interface method or delegate. The return value is not used and the like.

 

 Inverter example of an interface of:

  

public interface NiBian<in T>
    {
        void Print(T obj);
    }
    public class NiBianInstance<T> : NiBian<T>
    {

        public void Print(T obj)
        {
            Console.WriteLine(obj.GetType().Name);
        }
    }
 static void Main(string[] args)
        {
            NiBian<Chinese> niBian = new NiBianInstance<Person>();

            Console.ReadKey();
        }

 

======================================== ======== end of line =================================================

 

 

 

Guess you like

Origin www.cnblogs.com/norain/p/InversionAndCovariance.html