07.c# simple description of the limitations and nullable types of generics (three chapters 3.5-four 4.1)

Source: Internet
Author: User

Oneself in the writing article at the same time, is also in the study, for the book the statement many actually did not have the very good understanding, reads a book, MUST digest!!! The three chapters are all about generics, and finally write about the limitations of generics, for the end of this chapter of learning, one end,one begin.

Look at the code below.

1  Public classTeacher2     {3          Public enumFlag {Chinese, 中文版};4          Public stringName {Get;Set; }5     }6 7      Public classChineseteacher:teacher8     {9Flag f =Flag.chinese;Ten     } One  A      Public classEnglishteacher:teacher -     { -Flag f =Flag.english; the}

First of all, the variability can be divided into covariance and inverse degeneration. When a person simply understands that covariance acts on a return type, the returned object conforms to the return type in the signature and is converted upward, as a method returns a teacher object, when we can return a Englishteacher object. Because the Englishteacher class is inherited from the teacher class. Or the signature indicates that you want to return an interface, you can return an instance of the class that implements the interface. And for the inverse, it acts on the transmission of the parameter, if a method accepts a teacher object as a parameter, it can pass in a Chineseteacher object. Teacher said I compromised, you can pass in an implementation or inherit my line, do not know whether the correct, please correct me.

1. Under generics, generics do not support covariance, as the following code will make an error

1 New List<chineseteacher> {2                 new  chineseteacher (),3                  New  englishteacher ()4             };

The list object points to a list<teacher> listing, while the actual new is referring to the list<chineseteacher> list,list<chineseteacher> <teacher>, I wonder if I can understand it as a co-change? Please correct me. Adding a Chineseteacher instance to a list<chineseteacher> is certainly true, and it is certainly true that you are adding a Englishteacher object to list<teacher>. But it is certainly wrong to add a Englishteacher object to list<chineseteacher>, so you can see from the above ambiguity that generics do not support covariance.

2. Lack of operator constraints or "numeric" constraints

If we want to operate on a generic class instance operator in a generic method that does not work, and has a bug, the error occurs at compile time, and you can later use the dynamic type to skip the compilation phase and wait until the execution stage to make the decision.

--------------------------------------------------------------------------------------------------------------- ---------------------

In view of the fact that generics can be too short, of course, also know that the article in the essence is not many, said that there is no reference to the nullable type before, we are how to represent a "no value" value.

1. Discard a value, use this value to indicate "No value", when many people develop, it is like specifying ERRORCODE:1 indicates the server error, Errorcode:2 indicates that the user is not working correctly.

2. Use a decorative reference type to include a value, set a type of bool tag

3. A tag with a bool type relative to the value attribute differs from 2 in that the method of 3 is in the same class, while 2 is encapsulating the value and token into a class and adding an instance of the enclosing class to the class that uses the value.

Three ways of the shortcomings of self-evident, too much input code, can not be reused, based on this C # introduced a nullable type.

Please treatise.

07.c# simple description of the limitations and nullable types of generics (three chapters 3.5-four 4.1)

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.