- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 297字
- 2021-07-02 15:29:27
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 ())
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.
- Java程序設計(慕課版)
- Python機器學習:數據分析與評分卡建模(微課版)
- Azure IoT Development Cookbook
- Rust編程從入門到實戰
- 看透JavaScript:原理、方法與實踐
- Python數據可視化之Matplotlib與Pyecharts實戰
- 可解釋機器學習:模型、方法與實踐
- AutoCAD 2009實訓指導
- Arduino可穿戴設備開發
- Learning Python Data Visualization
- Mastering HTML5 Forms
- 人人都能開發RPA機器人:UiPath從入門到實戰
- Laravel Design Patterns and Best Practices
- 量子計算機編程:從入門到實踐
- 計算機程序的構造和解釋(JavaScript版)