"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 42: Using generic parameters to be compatible with the immutability of generic interfaces

Source: Internet
Author: User

Recommendation 42: Using generic parameters to be compatible with the immutability of generic interfaces

Let the return value type return a type that is more derived than the declared type, which is covariant. Such as:

         Public Employee Getaemployee (string  name)        {            Console.WriteLine (" I am an employee:" +name);             return New Programmer () {name = name}; // Programmer is a subclass of employee        }

Programmer is a subclass of employee, so the programmer object is also an employee object. The Getaemployee method returns an programmer object, which is the equivalent of returning an employee object.

Since covariance is such a natural application, we are likely to write the following code:

    classProgram {Static voidMain (string[] args) {Isalary<Programmer> s =NewBasesalarycounter<programmer>();        Printsalary (s); }static voidPrintsalary (isalary<Employee> s)
{
S.pay ();
} } InterfaceIsalary<t> { voidPay (); } classBasesalarycounter<t>: isalary<t> { Public voidPay () {Console.WriteLine ("Pay Base Salary"); } } classEmployee { Public stringName {Get;Set; } } classProgrammer:employee {}classManager:employee {}

In Printsalary this method, the type that the method receives is isalary<employee>. Therefore, we take for granted that isalary<programmer> must also be accepted by the Printsalary method. As a matter of fact, code compilation does not:

Cannot convert from "mytest.isalary<mytest.programmer>" to "mytest.isalary<mytest.employee>"

The compiler checks the interface and delegate type parameters very strictly, unless the keyword out is specifically declared, otherwise this code will only compile failed. To get printsalary to complete the requirements, we can use generic type parameters:

        Static void Printsalary<t> (isalary<t> s)        {            s.pay ();        }

Note: The recommendation begins by stating that "covariance" is for the return value, but this example does not reflect the concept of "return value". In fact, only a generic type parameter is not used as an input parameter to a method in an interface declaration, so let's think of it as a "return value" type. Therefore, this model in this recommendation satisfies the definition of "covariance". However, the "covariant" definition is not satisfied as long as the T is the input parameter.

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 42: Using generic parameters to be compatible with the immutability of generic interfaces

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.