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

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.

主站蜘蛛池模板: 旺苍县| 铁岭县| 绥化市| 宝鸡市| 桐梓县| 晋州市| 汉中市| 措勤县| 长阳| 汉沽区| 泸水县| 广安市| 巧家县| 绍兴市| 鄯善县| 武功县| 漳州市| 尚义县| 广州市| 扎囊县| 福泉市| 谢通门县| 登封市| 桦川县| 穆棱市| 峨边| 九龙坡区| 嫩江县| 怀仁县| 清河县| 芜湖市| 宣威市| 禄劝| 乐安县| 武安市| 太湖县| 平利县| 奉化市| 油尖旺区| 三门县| 澄城县|