Commissioned inverter and covariant

Source: Internet
Author: User

When the method signature matches the delegate type, covariant and contravariance provide a certain degree of flexibility. The covariant allows a method to have more derived return types than those defined in the delegate. The inverse method allows fewer derived parameter types than the delegate type.

Example 1 (covariant) Description

This example shows how to use a delegate with a method with a return type, which is derived from the return type in the delegate signature. The data type returned by secondhandler is the dogs type, which is derived from the mammals type defined in the delegate.

Class mammals
{
}

Class dogs: Mammals
{
}

Class Program
{
// Define the delegate.
Public Delegate mammals handlermethod ();

Public static mammals firsthandler ()
{
Return NULL;
}

Public static dogs secondhandler ()
{
Return NULL;
}

Static void main ()
{
Handlermethod handler1 = firsthandler;

// Covariance allows this delegate.
Handlermethod handler2 = secondhandler;
}
}


Example 2 (inverter) Description

This example shows how to use a delegate with a method with a certain type of parameters, which are the base type of the delegate signature parameter type. Several different procedures must be used in the pastProgramNow, you only need to use an event handler. For example, you can now create an event handler to receive parameters input by eventargs, And then you can send the handler and a button of the mouseeventargs type (as a parameter. the mouseclick event can be used together with the textbox that sends the keyeventargs parameter. the keydown event is used together.

 
System. datetime lastactivity;
 
Public form1 ()
 
{Initializecomponent ();
 
Lastactivity = new system. datetime ();
 
This. textbox1.keydown + = This. multihandler; // works with keyeventargs
This. button1.mouseclick + = This. multihandler; // works with mouseeventargs
 
} // Event hander for any event with an eventargs or
 
// Derived class in the second parameter
 
Private void multihandler (Object sender, system. eventargs E)
 
{Lastactivity = system. datetime. now;
 
This. label1.text = lastactivity. tostring ();
 
}

This sectionCodeIt seems that I didn't declare a delegate as usual, but don't forget that the event is a special delegate.

Therefore, this. textbox1.keydown and this. button1.mouseclick are actually two delegates, which encapsulate the same event handler.

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.