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

Making an anonymous function or method

Now, if you want, you can also do stuff that does not involve names. For example, you can enter the following next:

vals.ForEach(delegate (double x)

Next, we'll define the body, or the logic, between a set of curly braces. This is a nameless or anonymous one. For example, you can enter the following below this line (notice that you close with a parenthesis and semicolon after the closed curly brace):

{
    sampLabel.Text += "<br>" + Math.Pow(x, 3);
});

This one does something similar to the previous line. The only difference is that we are not calling anything named; we are just defining an anonymous function, a nameless function using a delegate keyword. This does accept one value, of course, the x value. Then you cube the x value; Math.Pow(x, 3) means, cube it and then display it on the label using += to append and <br> to push down a line, as usual.

Now, in the next stage, you can also do stuff such as the following, which is quite interesting:

Thread td = new Thread(delegate ())
Believe it or not, though it isn't recommended, after new Thread you can even can type dele instead of delegate, in this case.

Now, when you make an object of this type, you can also create a delegate. So, when you make this Thread object, you're also making an anonymous function. In other words, you're sending a piece of processing so that it runs on its own thread, and then you can stick in stuff such as the following:

{
List<double> arrs = new List<double>(new double[] { 1, 4, 5, 3, 53, 52 });arrs.Sort();arrs.ForEach(x => sampLabel.Text += $"<br>{x}");
});

Note again that here you close with a parenthesis and semicolon after the closing curly brace.

主站蜘蛛池模板: 自治县| 南乐县| 龙江县| 嘉峪关市| 浙江省| 丹东市| 广水市| 黄冈市| 丰顺县| 西平县| 贺州市| 威宁| 通榆县| 泉州市| 南丰县| 海城市| 五台县| 汉沽区| 旬邑县| 冕宁县| 闸北区| 永寿县| 吉林省| 宜宾县| 商丘市| 清河县| 岳阳市| 唐山市| 高密市| 延边| 无棣县| 新晃| 鄂尔多斯市| 梨树县| 临沂市| 廉江市| 永兴县| 宁乡县| 瓦房店市| 宁安市| 井研县|