C # What we should know from delegation to Lambda expressions -- why?

Source: Internet
Author: User

After the publication of the previous blog post, I am very pleased to receive the attention of many bloggers. It is very valuable for me to encourage and give some suggestions, in this article, we will talk about the convenience that lambda expressions bring to the use of routine delegation.
If we want to install an automation system for the company's data centers, the hardware is definitely not what we care about. Our task is to implement logical control, that is, when the system is started, we need to perform the corresponding operations. To do this, simply turn on the power, open the server and the service, we can use the C # Delegate to implement its basic logic. The following uses the console program as an example:
First, you need to define an interface that abstracts the operations that can be performed in the data center and inherits the class of this interface. It can be simply divided into opening and closing, here we use the image of OpenSomething or ShutdownSomething,
  
Public interface IOperation
{

}
 
Public class OpenSomething: IOperation
{

}
In order not to break away from the topic of this article, the interface here is just a form, not to add any code, just to reflect the face-to-face programming ideas. Next, we need to define a Monitor class to indicate the Monitor, which is used to control and display ongoing operations.
Public class Monitor
{
Public delegate void OpenEventHandler (); // defines the delegate for automatic control.
}
After the class is defined, it is the write method. In OpenSomething, the method we write is actually a virtual representation of the actual action, such as power on and server on, as follows:
 
/// <Summary>
/// Power on
/// </Summary>
Public void OpenPower ()
{
Console. WriteLine ("power on. \ r \ n ");
}

/// <Summary>
/// Open the server
/// </Summary>
Public void OpenServer ()
{
Console. WriteLine ("the server has enabled. \ r \ n ");
}

/// <Summary>
/// Open the service
/// </Summary>
/// <Param name = "timeOut"> set the timeOut time (unit: seconds) </param>
Public void OpenServices (int timeOut)
{
For (int I = timeOut; I> 0; I --)
{
Console. WriteLine ("the service will be enabled in {0} seconds! \ R \ n ", I );

Thread. Sleep (1000 );
}
Console. WriteLine ("service enabled. \ r \ n ");
}
 
In this way, we can write code in the main function for execution.
 
Class Program
{
Static void Main (string [] args)
{
Monitor. OpenEventHandler OnOpen; // defines a delegate variable

OpenSomething openSomething = new OpenSomething (); // instantiate an object for enabling

OnOpen = openSomething. OpenPower;

OnOpen + = openSomething. OpenServer;

OnOpen ();

Console. ReadKey ();
}
}
 
The program is compiled and run correctly under VS2008.
 
However, we can see that in the OpenSomething method, the OpenServices method requires parameter input, and the delegate we define is a non-parameter, if you want to pass the reference of a function with parameters to a variable of the delegate type without parameters, an error will be reported during compilation. At this time, there are two methods that will easily appear in our minds: 1. define a new delegate and accept an int type parameter 2. Add a method in OpenSomething to call OpenServices and input a given value, as shown below:
 
/// <Summary>
/// Open the service www.2cto.com immediately
/// </Summary>
Public void OpenServicesImediately ()
{
OpenServices (0); // input a given value, 0;
}
 
Then, the main function can directly assign this function reference to the delegate variable. However, in this case, the flexibility of the program will be greatly reduced. If you need different start times, you may need to write different methods. This is what we don't want to see. What should we do?
Perhaps it is based on this. Microsoft has introduced a new technology called Lambda expressions to a wide range of. Net programmers. With it, we can do this.
 
Class Program
{
Static void Main (string [] args)
{
Monitor. OpenEventHandler OnOpen; // defines a delegate variable

OpenSomething openSomething = new OpenSomething (); // instantiate an object for enabling

OnOpen = openSomething. OpenPower;

OnOpen + = openSomething. OpenServer;

OnOpen + = () => openSomething. OpenServices (5); // the reference of the method is still assigned to the delegate variable.

OnOpen ();

Console. ReadKey ();
}
}
 
As you can see, in the 9th line of code, we can use a Lambda expression to pass parameters to the OpenServices method, and pass the reference of the method to the Delegate for the delegate to call, is it much more convenient.
Running result:
 

 
I shared this small knowledge point and hoped to be helpful to the bloggers. I also hoped that my friends in the garden could give some suggestions so that I could learn more and improve myself.

 

 

From white light

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.