- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 527字
- 2021-07-02 15:29:24
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:

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:

- Deploying Node.js
- Mastering OpenCV Android Application Programming
- Learning C++ Functional Programming
- oreilly精品圖書:軟件開發(fā)者路線圖叢書(共8冊(cè))
- 深度學(xué)習(xí):算法入門與Keras編程實(shí)踐
- Responsive Web Design by Example
- Python數(shù)據(jù)分析從0到1
- Java程序設(shè)計(jì)
- The Professional ScrumMaster’s Handbook
- Julia 1.0 Programming Complete Reference Guide
- ExtJS Web應(yīng)用程序開發(fā)指南第2版
- Learning JavaScript Data Structures and Algorithms(Second Edition)
- 算法秘籍
- Raspberry Pi Blueprints
- Blender 3D Cookbook