Co-Variation and inverter in delegation (C # programming guide)

Source: Internet
Author: User

When the method signature matches the delegate type, the covariant and inverter provide you with 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.

I. covariant
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. BySecondHandlerThe returned data type isDogsType, which is defined by the delegateMammalsType.

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;
}
}

Ii. Inverter

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. Through the inverter, a number of different processing procedures must be used before, and now only one event processing program can be used for failover. 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;
}

Source: MSDN

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.