157 recommendations for writing high-quality code to improve C # programs--Recommendation 44: Understanding covariance in delegates

Source: Internet
Author: User

Recommendation 44: Understanding the covariance in a delegate

The generic variable in the delegate is partially supported by the covariance. Why is "partial support covariance"? Look at the following example:

   classProgram { Public DelegateT getemployeehanlder<t> (stringname); Static voidMain () {Getemployeehanlder<Employee> Getaemployee =Getamanager; Employee e= Getaemployee ("Mike"); }        StaticManager Getamanager (stringname) {Console.WriteLine ("I am the manager:"+name); return NewManager () {Name =name}; }        StaticEmployee Getaemployee (stringname) {Console.WriteLine ("I am an employee:"+name); return NewEmployee () {Name =name}; }    }    InterfaceIsalary<t>    {        voidPay (); }    classBasesalarycounter<t>: isalary<t>    {         Public voidPay () {Console.WriteLine ("Pay Base Salary"); }    }    classEmployee { Public stringName {Get;Set; } }    classProgrammer:employee {}classManager:employee {}

The Getamanager in the above returns a manager, but in use, it is actually assigned to a delegate variable whose generic parameter is employee. Because of the following situation, compiling is not possible:

            getemployeehanlder<manager> Getamanager = Getamanager;            Getemployeehanlder<Employee> getaemployee = Getamanager;

To get the above code to compile, you also need to specify the OUT keyword for the generic parameter in the delegate:

 Public Delegate T getemployeehanlder< out t> (string name);

Unless the delegate declaration is certainly not used for variability, specifying the OUT keyword for a generic parameter in a delegate expands the application of the delegate and is recommended to be used forever in the actual encoding process. In fact, some of the delegate declarations in FCL4.0 have used the OUT keyword to allow delegates to support covariance, as we would often use:

 Public Delegate TResult func< out tresult>();  Public Delegate TOutput converter< in Out toutput> (tinput input);

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

157 recommendations for writing high-quality code to improve C # programs--Recommendation 44: Understanding covariance in delegates

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.