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

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.

主站蜘蛛池模板: 浦江县| 安乡县| 信宜市| 监利县| 凌海市| 昭通市| 巫溪县| 汕头市| 吉水县| 天津市| 九龙县| 嘉善县| 绿春县| 嵩明县| 和林格尔县| 临西县| 卫辉市| 和田市| 延寿县| 罗山县| 屏山县| 沛县| 涪陵区| 古浪县| 沂南县| 茶陵县| 增城市| 乌鲁木齐县| 榆社县| 闵行区| 彭山县| 鹤山市| 滨海县| 永和县| 怀安县| 南部县| 丰城市| 普洱| 工布江达县| 深水埗区| 武宣县|