Resistance to changes and coordination of generic interfaces, generic interfaces and coordination

Source: Internet
Author: User

Resistance to changes and coordination of generic interfaces, generic interfaces and coordination

1. covariant of generic Interfaces

If the generic type is marked with the out keyword, the generic interface is the covariant. This also means that the return type can only be T.

Resistance to changes to generic Interfaces

If the generic type is marked with the in keyword, the generic interface is variable-resistant. In this way, the interface can only use generic type T as the input of its method, that is, the parameter of the method.

This is the definition of resistance to changes and coordination of generic interfaces. Let's use the code below to describe the Code directly,

1 /// <summary> 2 /// generic interface 3 /// </summary> 4 /// <typeparam name = "T"> </typeparam> 5 public interface IDisplay <T> 6 {7 void Show (T item ); 8} 9 10 /// <summary> 11 // implement the generic interface IDisaplay12 /// </summary> 13 /// <typeparam name = "T"> </ typeparam> 14 public class ShapDisplay <T>: IDisplay <T> 15 {16 public void Show (T item) 17 {18 Console. writeLine ("test successful! "); 19} 20} 21 22 // <summary> 23 // parent class 24 /// </summary> 25 public class ParentClass26 {27} 28 29 // <summary> 30 // SubClass 31 /// </summary> 32 public class SubClass: parentClass33 {34}

 

2. The interface and the implemented interface are defined above. Next, we will test the class that implements the interface.

1 class Program 2 {3 static void Main (string [] args) 4 {5 // instantiate a generic class with a subclass (referred to as a subclass object) 6 IDisplay <SubClass> sub1 = new ShapDisplay <SubClass> (); 7 8 // instantiate the generic class with the parent class (referred to as the parent class Object) 9 IDisplay <ParentClass> par1 = new ShapDisplay <ParentClass> (); 10 11 // receives subclass objects (subclass objects → parent classes) with the parent class type) covariant 12 IDisplay <ParentClass> parent = sub1; 13 14 // use the SubClass type to receive the parent class Object (parent class Object → SubClass type) and change the value to 15 IDisplay <SubClass> sub = par1; 16 17 Console. readKey (); 18 19} 20}

We will find that errors will be reported in lines 12 and 15 of the Code. The compilation is not complete. Why?

The reason is very simple, because when we define an interface like this at the top, there is no addition of out or in, that is, the generic interface does not support resistance to changes and coordination by default, so an error will be reported during compilation.

Okay, let's modify the following code for the generic interface:

1 /// <summary> 2 /// generic interface 3 /// </summary> 4 /// <typeparam name = "T"> </typeparam> 5 public interface IDisplay <out T> 6 {7 void Show (T item ); 8}

After an out clause is added before the generic type, the Show clause in the interface returns an error. Why?

According to the covariant of the generic interface, if the generic type is marked with the out keyword, this means that the returned type can only be T. That is to say, the return type of the method should be T, while in the Show method, the parameter of the method is T, so it does not comply with the rules and an error is returned.

Then let's modify the Code as follows:

1 /// <summary> 2 /// generic interface 3 /// </summary> 4 /// <typeparam name = "T"> </typeparam> 5 public interface IDisplay <in T> 6 {7 void Show (T item ); 8}

There is no problem with the interface. However, the following error is still reported in line 12 of the callback and main methods: wtf?

Because the generic type is marked with in, this indicates that this generic type only supports resistance to changes, and 12 lines of code are covariant, so an error is reported.

At this point, the resistance to changes and the coordination of generic interfaces are explained. The following three points are summarized,

① For a generic interface, if there is no keyword out or in before the generic type, the generic interface does not support resistance to changes and coordination, that is, only what object points to what type.

② If there is an out keyword before a generic interface, the output of the method is T type, that is, the return value of the method. At the same time, this generic interface supports covariant, that is, the type of the parent class can be used to point to the object of the subclass.

③ If a generic interface is marked with the keyword in before the generic type, the input of the method is T type, that is, the parameter of the method. This generic interface supports variable resistance, that is, objects of the parent class can be pointed to by the subclass type.

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.