- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 395字
- 2021-07-02 15:29:23
Defining lambda expressions
Now, to define a lambda expression, you put = (a,b), as shown. Then this will be mapped to the operations that follow; so you think of => as the mapping symbol or mapping operator. It'll be mapped to the (a==b) operation. So, comp, in other words, will allow us to check whether the two values are the same, and that happens in the stage right where a and b are compared. Basically, (a, b) are the parameters, and the expression that is evaluated is whether a is equal to b.
Now, enter the following next:
sampLabel.Text = $"{x} and {y} are equal is {comp(x, y).ToString().ToLower()}";
To invoke this, note that you type comp and then pass in the x and y values. Then, to show that you can operate on this further, once you get a result from it, you can convert it, say, to a string version, and then all to lowercase, as shown in the preceding code line.
Remember, this is function chaining, so it goes from left to right as it executes. In other words, first comp runs, then ToString and lastly ToLower.
Also, note that, at runtime, when you pass in the x and y values when comp(x, y) is called, basically, it is (a==b) that will be hit; the comparison will be made, and the value will be sent back.
Next, we can also do the Multiply delegate, so enter the following below this line:
Multiply mult = (a, b) => (a * b);
Notice that (a,b) can be used and reused and so on. Remember that (a,b) here are parameters, and you can use them and reuse them. They're local within each of the lines where they appear. So, then you can use it in another one. Then, you say again that (a,b) maps to an operation of (a*b). Close this with a semicolon.
Now, to invoke this multiplication delegate (Lambda expression that it represents), copy (Ctrl + C) the sampLabel.Text line from above and paste it (Ctrl + V) down below, as shown here:
sampLabel.Text += $"<br>{x}*{y} is {mult(x, y).toString()}";
Here, we say {x}*{y} instead and then, += to append, and delete are equal, and replace comp with mult as the name of our object. You don't need toString for it to work, and since it'll give back a number, you don't need ToLower either.
- CockroachDB權威指南
- MATLAB圖像處理超級學習手冊
- Android 7編程入門經典:使用Android Studio 2(第4版)
- Julia高性能科學計算(第2版)
- 精通Python自動化編程
- 從零開始:UI圖標設計與制作(第3版)
- Mastering Apache Storm
- SQL Server 2008中文版項目教程(第3版)
- Backbone.js Testing
- MySQL 8從零開始學(視頻教學版)
- Anaconda數據科學實戰
- Continuous Delivery and DevOps:A Quickstart Guide Second Edition
- HTML5游戲開發實戰
- React and React Native
- C++標準庫(第2版)