07. C # Brief description of generic restrictions and empty types (Chapter 3, chapter 2-Chapter 4, Chapter 3.5 ),

Source: Internet
Author: User

07. C # Brief description of generic restrictions and empty types (Chapter 3, chapter 2-Chapter 4, Chapter 3.5 ),

While writing articles, I am also learning. I don't have a good understanding of many statements in the book. Read a book and digest it !!! The three chapters are about generics. Finally, let's write the limitations on generics. for the end of this chapter, one end, one begin.

See the following code.

 1 public class Teacher 2     { 3         public enum Flag { Chinese, English }; 4         public string Name { get; set; } 5     } 6  7     public class ChineseTeacher : Teacher 8     { 9         Flag f = Flag.Chinese;10     }11 12     public class EnglishTeacher : Teacher13     {14         Flag f = Flag.English;15     }

Let's first talk about the variability. The variability can be divided into the covariance and the inverter. When a person simply resolves this as a covariance to act on the return type, the returned object conforms to the return type in the signature and is converted upwards. For example, if a method returns a Teacher object, we can return an EnglishTeacher object, because the EnglishTeacher class inherits from the Teacher class. Or if the signature indicates that an interface is to be returned, an instance of the class implementing this interface can be returned. If a method accepts a Teacher object as a parameter, it can pass in a ChineseTeacher object. Teacher said that I had compromised. You can pass in an implementation or inherit from me. I don't know whether it is correct. please correct me.

1. Let's talk about generics. Generics do not support covariant. The following code will go wrong:

1 List<Teacher> list = new List<ChineseTeacher> {2                 new ChineseTeacher(),3                 new EnglishTeacher()4             };

The list object points to a List <Teacher> List, while the List <ChineseTeacher> List, list <ChineseTeacher> to List <Teacher>, I don't know if it can be understood as covariant? Please correct. Adding a ChineseTeacher instance to a List <ChineseTeacher> is certainly correct, and adding an EnglishTeacher object to the List <Teacher> is certainly true, however, adding an EnglishTeacher object to the List <ChineseTeacher> is definitely not correct. Therefore, we can see that the generic type does not support covariant.

2. Lack of operator or numerical Constraints

For example, if we want to perform operator operations on a generic class instance in a generic method, it will not work and there is a BUG, and an error will be reported during compilation, in the future, dynamic types can be used to skip the compilation phase and wait until the execution phase before making decisions.

Certificate ------------------------------------------------------------------------------------------------------------------------------------

In view of the fact that the article is too short to be generic, we also know that there are not many excellent articles, but how do we express a value with "no value" before the article does not reference a null type.

1. discard a value, which indicates "no value". When multiple developers develop, it is like specifying errorCode: 1 indicates a server error and errorCode: 2 indicates that the user is not performing the operation correctly.

2. Use a decoration reference type to include a value and set a bool type mark

3. A bool type tag is added relative to the value attribute. The difference from 2 is that 3 is in the same class, and 2 is to encapsulate the value and tag into a class, add an instance of the closed class to the class using the value.

The disadvantage of the three methods is self-evident. If you enter too much code and cannot use it again, this C # introduces the void type.

 

Please make an axe.

 

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.