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

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
主站蜘蛛池模板: 徐汇区| 海安县| 沙雅县| 古蔺县| 高邑县| 九江市| 芦山县| 越西县| 贺兰县| 乐亭县| 静海县| 定州市| 九台市| 浦城县| 宁远县| 万年县| 江津市| 商丘市| 雅安市| 恩平市| 十堰市| 延寿县| 西和县| 舞钢市| 宿州市| 鄂伦春自治旗| 晋江市| 黑山县| 吉木萨尔县| 库尔勒市| 宁城县| 屯门区| 武汉市| 额尔古纳市| 弥勒县| 吉林省| 青州市| 米林县| 江华| 娄底市| 澄城县|