- 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:

- 程序員面試白皮書
- Learning AWS Lumberyard Game Development
- Java游戲服務(wù)器架構(gòu)實(shí)戰(zhàn)
- Spring+Spring MVC+MyBatis整合開發(fā)實(shí)戰(zhàn)
- Python時(shí)間序列預(yù)測(cè)
- Python忍者秘籍
- 劍指Java:核心原理與應(yīng)用實(shí)踐
- Learning Unreal Engine Android Game Development
- Java零基礎(chǔ)實(shí)戰(zhàn)
- IBM Cognos TM1 Developer's Certification guide
- C陷阱與缺陷
- Oracle實(shí)用教程
- 從零開始學(xué)算法:基于Python
- 軟硬件綜合系統(tǒng)軟件需求建模及可靠性綜合試驗(yàn)、分析、評(píng)價(jià)技術(shù)
- Design Patterns and Best Practices in Java