- 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.
- The Android Game Developer's Handbook
- Java 開發從入門到精通(第2版)
- Vue.js前端開發基礎與項目實戰
- 算法大爆炸:面試通關步步為營
- INSTANT MinGW Starter
- MySQL數據庫管理與開發(慕課版)
- Haskell Data Analysis Cookbook
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- Go語言入門經典
- 3D Printing Designs:The Sun Puzzle
- Python AI游戲編程入門:基于Pygame和PyTorch
- Java程序設計
- iOS程序員面試筆試真題與解析
- Swift 5從零到精通iOS開發訓練營
- 梔子貓的奇幻編程之旅:21天探索信息學奧賽C++編程