Beginning C# 7 Hands-On:Advanced Language Features
上QQ阅读APP看书,第一时间看更新

Working with actions

Now, we will talk about actions, so enter the following as the next line:

Action<double> showDouble = (a) => sampLabel.Text += "<br>" + (a * a);

Notice that Action is a delegate. So, if you right-click on Action and select Go To Definition, you'll see public delegate void Action(). If you expand it, it says, Encapsulates a method that has no parameters and does not return a value. This is the essential definition of an action in .NET.

You can extend an Action delegate, however. They can be generic. For example, if you type Action<double> and right-click on it and select Go To Definition again, this particular form does take a parameter. If you expand it, the Parameters section says, The parameter of the method that this delegate encapsulates. Further, the Summary section says, Encapsulates a method that has a single parameter and does not return a value. So, again, there's no need to guess. Right-click and select Go To Definition or hover your mouse over it. It tells you what you need to know. In our case, it will actually be showDouble as seen in the preceding line. Now, another lambda can be used to define this; so you insert (a) there as a single parameter, then, enter the mapping symbol =>, and then, sampLabel.text. You want to append this to the existing text, so type +=, and then, you say, <br>, and then show the square of a, type + (a * a) and close with a semicolon.

Now remember from the definition of Actions, they do not return a value, right? In fact, if we type Action<double>, and look at the pop-up tip, if you go through the entire list up through T16, it says, Encapsulates a method that has 16 parameters and does not return a value, as shown in Figure 6.1.2:

Figure 6.1.2. None of the actions return a value after typing Action<double>,

So, none of them return a value. This is a basic feature of Actions as they are defined here, but remember that ultimately it's just a delegate.

Then, for example, to make use of these Actions, one thing that you can do is to enter the following:

foreach(var d in dubsArray)

In the next stage, enter the following between a set of curly braces below this line to invoke the actions:

showDouble(d);

These are the basics of working with delegates and Lambda expressions. The two delegates at the top of the file are the heart of our program, followed by Compare and Multiply, which are the delegate types being used down below, and then the Lambda expressions, which are parameter expressions, such as (a, b) => (a == b), (a, b) => (a * b), and (a) => sampLabel.Text += "<br>" + (a * a), which are defined using those delegates.

Now, take a look at this in your browser. Click on the Show Results button. It says, 10 and 25 are equal is false and 10*25 is 250, and then the squares are printed. These are the basic results, and everything looks as it's supposed to look:

Figure 6.1.3. The results of running our program