官术网_书友最值得收藏!

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
主站蜘蛛池模板: 台前县| 万载县| 简阳市| 屯门区| 民乐县| 阳西县| 南丰县| 新营市| 湘潭县| 虞城县| 迁西县| 曲松县| 卢湾区| 通许县| 阿拉善盟| 肥乡县| 昌都县| 咸宁市| 西乌珠穆沁旗| 嘉禾县| 庆阳市| 南汇区| 长兴县| 许昌县| 蕲春县| 西林县| 察雅县| 伊宁县| 四子王旗| 康乐县| 邢台县| 阿尔山市| 阳山县| 临颍县| 柳州市| 临邑县| 衢州市| 乐业县| 彭泽县| 莆田市| 沾益县|