当前位置:网站首页>C # [advanced chapter] C # anonymous method [lambda expression to be supplemented...]
C # [advanced chapter] C # anonymous method [lambda expression to be supplemented...]
2022-06-30 03:20:00 【Tomorrow is like noon】
C#【 Advanced 】 C# Anonymous methods
Preface
We have already mentioned , A delegate is used to refer to a method that has the same label as it . let me put it another way , You can use a delegate object to call methods that can be referenced by a delegate .
Anonymous methods (Anonymous methods) Provides a The technique of passing code blocks as delegate parameters . The anonymous method is A method with no name but a body .
In anonymous methods, you You do not need to specify a return type , It's from within the method body return The sentence infers .
Syntax for writing anonymous methods
The anonymous method is by using delegate Keyword to create a delegate instance to declare . for example :
delegate void NumberChanger(int n);
...
NumberChanger nc = delegate(int x)
{
Console.WriteLine("Anonymous Method: {0}", x);
};
Code block Console.WriteLine(“Anonymous Method: {0}”, x); yes The body of an anonymous method .
Delegates can be called through anonymous methods , You can also call , namely , By passing method parameters to the delegate object .
Be careful : The body of an anonymous method needs a ;.
for example :
nc(10);
example
The following example demonstrates the concept of anonymous methods :
using System;
delegate void NumberChanger(int n);
namespace DelegateAppl
{
class TestDelegate
{
static int num = 10;
public static void AddNum(int p)
{
num += p;
Console.WriteLine("Named Method: {0}", num);
}
public static void MultNum(int q)
{
num *= q;
Console.WriteLine("Named Method: {0}", num);
}
static void Main(string[] args)
{
//【01】 Create delegate instances using anonymous methods
NumberChanger nc = delegate (int x)
{
Console.WriteLine("Anonymous Method: {0}", x);
};
// 【01】 Using delegates to call anonymous methods
nc(10);
// 【02】 Instantiate the delegate using the name
nc = new NumberChanger(AddNum);
// 【02】 Using delegates to call named methods
nc(5);
// 【03】 Instantiate the delegate using another naming method
nc = new NumberChanger(MultNum);
// 【03】 Using delegates to call named methods
nc(2);
Console.ReadKey();
}
}
}
Running results :
Anonymous Method: 10
Named Method: 15
Named Method: 30
summary
- About anonymous methods : It will be used in practice , And can understand the code written by others .
边栏推荐
- An article to get you started VIM
- The broadcast module code runs normally in autojs4.1.1, but an error is reported in pro7.0 (not resolved)
- Auto.js学习笔记16:按项目保存到手机上,不用每次都保存单个js文件,方便调试和打包
- Neo4j--- performance optimization
- NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
- ZABBIX trigger explanation
- 什么是外链和内链?
- Use of foreach in QT
- 4-4 beauty ranking (10 points)
- 快速排序、聚簇索引、尋找數據中第k大的值
猜你喜欢

专升本高数(三)
![[ten minutes] manim installation 2022](/img/54/7b895d785c7866271f06ff49cb20aa.png)
[ten minutes] manim installation 2022

Cross domain, CORS, jsonp
![[qt] qmap usage details](/img/ee/6e71a3dc5b90d2d1b7f7d3f6b56221.png)
[qt] qmap usage details

A GPU approach to particle physics

Auto. JS learning notes 16: save to the mobile phone by project, instead of saving a single JS file every time, which is convenient for debugging and packaging

O & M (21) make winpe startup USB flash disk
![[wechat applet] how did the conditional rendering list render work?](/img/db/4e79279272b75759cdc8d6f31950f1.png)
[wechat applet] how did the conditional rendering list render work?

约瑟夫环 数学解法

产品思维 | 无人机快递的未来值得期待吗?
随机推荐
unity input system 使用记录(实例版)
F1c100s self made development board debugging process
O & M (21) make winpe startup USB flash disk
Use of foreach in QT
Gulang bilibilibili Live Screen Jackie
zabbix 触发器详解
OP diode limit swing
Global and Chinese market of bronze valves 2022-2028: Research Report on technology, participants, trends, market size and share
Mysql提取表字段中的字符串
发现mariadb数据库时间晚了12个小时
Are you a "social bull" or a "social terrorist" in the interview?
How do I enable assembly binding logging- How can I enable Assembly binding logging?
产品思维 | 无人机快递的未来值得期待吗?
1150_ Makefile learning_ Duplicate name target processing in makefile
Global and Chinese market of ULTRACENTRIFUGES 2022-2028: Research Report on technology, participants, trends, market size and share
2022 new test questions for safety management personnel of metal and nonmetal mines (small open pit quarries) and certificate examination for safety management personnel of metal and nonmetal mines (s
O & M (20) make and start USB flash disk and install win10
Openssl3.0 learning 22 provider decoder
专升本高数(四)
Neo4j---性能优化