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

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.

主站蜘蛛池模板: 洪雅县| 营山县| 扎兰屯市| 古浪县| 卓尼县| 陇南市| 会同县| 江源县| 灵寿县| 开化县| 顺平县| 渝北区| 洮南市| 武鸣县| 长宁区| 绵阳市| 东海县| 呼图壁县| 罗江县| 青铜峡市| 余干县| 庆安县| 老河口市| 神木县| 朝阳市| 伊川县| 南澳县| 呼图壁县| 兰坪| 清苑县| 丰台区| 梅河口市| 商丘市| 祁阳县| 华宁县| 越西县| 淮安市| 武川县| 汉沽区| 清河县| 城市|