Generic and antiallergic covariant (II)

Foreword

  Prior to .NET 4, generic interface is unchanged. .NET 4 and by anti covariant generic interface and generic delegate become an important extension added. Antiallergic and covariance parameters and refers to the type of the return value is converted.

  We look at what is in the end what is covariant resistance to change: If a return type can be replaced by its base class, then this is the type of support covariant   If a parameter type can be replaced by a derived class, then this type It is to support the inverter (resistance to change) of.
  

Type conversion function

  Before understanding the covariant and resistance to change, we look at the following example:

  class Program
     {
        public static string Tmain(object o)
        {
            return "aaa";
        }

        static void Main(string[] args)
        {
            string a = "aaa";
            object b = Tmain(a);
        }

     }

 

  We carefully look at this and return to traditional values. Note which found two implicit conversions.

    1, when the parameter value is a transfer function from a string type to convert the object type

    2, when the return value last received by a string type to convert b object type

  In return we look at the function.

    1, String Tmain (object o) can be converted into a string Tmain (string o)

    2, String Tmain (string o) can be converted to object Tmain (string o)

  Here, that is the time function of the input from the input object type can be converted into a string. Base class - derived class

  When the output function, the output function of the type (return type) to convert from a string object. Derived classes - the base class.

  Here closer to covariant generic interface and antiallergic the concept. We look at the beginning of our concept

 

  If a return type may be replaced by the base class, then the type is supported covariant   If a parameter type may be replaced by its derived class, it is to support this type of inverter (antiallergic) a.

 

Understood that generic interface covariant and antiallergic (in, out)

  Here we take a look at an example of covariance and anti-generic interface change:

  First, we look covariant in C # Advanced Programming (Eleventh Edition) pointed out that if the generic type marked with the out keyword, generic interface is covariant. This means that only the return type T.

    ///  <Summary> 
    /// identifier out, it means returning only type T
     ///  </ Summary> 
    ///  <typeParam name = "T"> </ typeParam> 
    interface Itest, < OUT T> 
    { 
        T Tmain ( Object value); 
    } 

 
    public  class the Test: Itest, < String > 
    { 
        public  String Tmain ( Object value) 
        { 
            return value.toString (); 
        } 
 }

 

We call upon:

 static void Main(string[] args)
        {
            Itest<string> itest = new Test();
            Itest<object> itestObj = itest;
        }

 

  Here, we finally received when the return value, should be received by the string type, but here we can modify, replace its base class object type. That is, a return type may be replaced by the base class, i.e. the support covariant. Note that the key point. Return type, derived class replaced by the base class.

 

 

  Then we look Look resistance to change can also be called an inverter. It pointed out that high-level programming in C # concept : If a generic type in a keyword tagging, generic interface is the resistance to change. Thus, the interface can be a generic type T as an input method thereof.

  /// <summary>
    /// 标识in,意味着输入类型只能是T
    /// </summary>
    /// <typeparam name="T"></typeparam>
    interface Itest<in T>
    {
        string Tmain(T value);
    }


    public class Test : Itest<object>
    {
        public string Tmain(object value)
        {
            return value.ToString();
        }
    }

 
    class Program
    {
        static void Main(string[] args)
        {
            Itest<object> itest = new Test();
            Itest<string> itestStr= itest;
        }
}

 

 

  Here we look at the example above, where the return type is already fixed a string type. The generic type generic interface used as a parameter passed. When we look at the call, normal type of parameters passed in object ,, but we modify the incoming parameters of type string types it is also possible. This is what we in the incorporation parameters, parameter can be replaced by a derived class, then this class is to support resistance to change (the inverter) is. Note The key point. Passing parameters, the derived class replace the base class.

to sum up

  In fact, in the above example and the concept, we can find, and antiallergic covariant generic interface, i.e. the type of the return parameters passed in the case or, for the law of the type implicit conversion followed in this case .

    Covariant :( keyword out) Returns the class type may be replaced by the base when the support is covariant.

    Resistance to change (the inverter) :( keyword in) passed parameter types can be replaced by their time of derived classes, is to support the resistance to change (the inverter) is.

 

  Husband learn to be quiet too, will be required to learn also, no non-school only to broad, non-chi no to school ------- Liang

 


 

  Welcome to scan the next Fanger Wei code, and the more I learn C # knowledge together

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/hulizhong/p/11239743.html