C # understanding of inverter and covariant ----- Why can inverter convert Object type parameters to String type parameters,

Source: Internet
Author: User

C # understanding of inverter and covariant ----- Why can inverter convert Object type parameters to String type parameters,

According to the variability rules, only interfaces and delegation can mark the variability. The variability can be used only when the type parameter is of the reference type.

Immutability: parameters of the generic type cannot be changed. This indicates that when an object of the MyInterface <String> type is created, only the MyInterface <String> type can be assigned to it.

1 namespace ContravariantAndCovariant 2 {3 public interface MyInterface <T> // if no variability is specified, the type parameters of this interface are unchanged 4 {5 void Show (T t ); 6} 7 8 public class ShowObject: MyInterface <Object> 9 {10 public void Show (object t) 11 {12 Console. writeLine (t. toString (); 13} 14} 15 16 public class ShowString: MyInterface <String> 17 {18 public void Show (string t) 19 {20 Console. writeLine (t); 21} 22} 23 24 public class Show Int: MyInterface Int32 <> 25 {26 public void Show (int t) 27 {28 Console. writeLine (t. toString (); 29} 30} 31 32 class Program33 {34 static void Main (string [] args) 35 {36 // correct 37 MyInterface <String> str_invariant = new ShowString (); // only values of MyInterface <String> type 38 // error 39 MyInterface <String> str_contravariant = new ShowObject (); // when values of other types are assigned, the compiler reports an error 40 // CS0266. The type "ContravariantAndCovariant. showObject is implicitly converted to C OntravariantAndCovariant. MyInterface <string> ". There is an explicit conversion (is forced conversion missing ?) 41 42} 43} 44 45}

Inverter: generic type parameters can be converted from a class to its derived class. For example, you can convert MyInterface <object> to MyInterface <string>. The inverter is represented by in, because it appears at the input position (method parameter), in means to go in.

1 namespace ContravariantAndCovariant 2 {3 public interface MyInterface <in T> // use the in keyword to specify T's Inversion 4 {5 void Show (T t ); // T is the parameter type 6} 7 8 public class ShowObject: MyInterface <Object> 9 {10 public void Show (object t) 11 {12 Console. writeLine (t. toString (); 13} 14} 15 16 public class ShowString: MyInterface <String> 17 {18 public void Show (string t) 19 {20 Console. writeLine (t); 21} 22} 23 24 public class ShowInt: MyInterface <Int32> 25 {26 public void Show (int t) 27 {28 Console. writeLine (t. toString (); 29} 30} 31 32 class Program33 {34 static void Main (string [] args) 35 {36 MyInterface <object> obj = null; 37 MyInterface <string> str = obj; // you can convert the MyInterface <object> type to MyInterface <string> type 38} 39} 40 41}

Covariance: a generic type parameter can be changed from a class to its base class. For example, you can convert MyInterface <string> to MyInterface <object>. The inverter is represented by "out" because it appears at the output position (Return Value of the method). "out" means output.

1 namespace ContravariantAndCovariant 2 {3 public interface MyInterface <out T> // use the out keyword to specify the covariance of T 4 {5 T Show (); // T is the return type 6} 7 8 public class ShowObject: MyInterface <Object> 9 {10 public object Show () 11 {12 return null; 13} 14} 15 16 public class ShowString: MyInterface <String> 17 {18 public string Show () 19 {20 return null; 21} 22} 23 24 class Program25 {26 static void Main (string [] args) 27 {28 // error 29 // MyInterface <object> obj = null; 30 // MyInterface <string> str = obj; // The MyInterface <object> type cannot be converted to MyInterface <string> type 31 32 // correct 33 MyInterface <string> str = null; 34 MyInterface <object> obj = str; // you can convert the MyInterface <string> type to MyInterface <Object> type 35} 36} 37}

CLR should ensure that the type conversion is secure and covariant is easy to understand. Subclass can be converted to the parent class because the subclass object contains all the members of the parent class object. This conversion is safe.

However, it is not safe to convert the parent class to a subclass. If the conversion cannot be performed by ryth, the parent class object does not contain all the members of the subclass.

In fact, there is a misunderstanding about references and objects. For example, in the code above:

MyInterface <object> obj = null;

MyInterface <string> str = obj;

Str. Show ("123 ")

Str is a variable of the MyInterface <string> type, but it references the Object of the MyInterface <Object> type. So when we call the Show () method, it is actually obj. show ();

The obj parameter is of the object type. We pass in a string type, and the conversion of string ----> object is safe. So the inverter is safe!

Similarly, covariant:

MyInterface <string> str = null;

Myinterface <object> obj = str;

Object result = obj. show ();

We can see that obj is a variable of the MyInterface <object> type, but it references the object of the MyInterface <string> type. So when we call the Show () method, it is actually str. show (),

It returns the string type, but obj. show () returns an object type. Therefore, we use the result variable of the object type to receive the returned value and convert it to the string -----> object type conversion. Therefore, the covariant is safe.

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.