当前位置:网站首页>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 .
边栏推荐
- 快速排序、聚簇索引、寻找数据中第k大的值
- mysql 主从数据库同步失败的原因
- What are the defaults for Binding. Mode=Default for WPF controls?
- Knowledge points of 2022 system integration project management engineer examination: software quality assurance and quality evaluation
- Add a custom button to jvxetable
- 发现mariadb数据库时间晚了12个小时
- unity input system 使用记录(实例版)
- 如何实现远程协同办公,收好这份攻略!
- 炒现货黄金的交易平台如何保障资金安全?
- JS conversion of letters and numbers
猜你喜欢
随机推荐
Tri rapide, index groupé, recherche de la plus grande valeur K dans les données
Wechat applet +php to realize authorized login operation
Customize the buttons of jvxetable and the usage of $set under notes
Use of custom MVC
如果辨别我现在交易的外盘股指期货交易平台是否正规安全?
浅谈IDEA的优化和使用
Global and Chinese market for sensor screwdrivers 2022-2028: Research Report on technology, participants, trends, market size and share
Learning cyclic redundancy CRC check
Regular full match: the password consists of more than 8 digits, upper and lower case letters, and special characters
Tp6 framework integrates JWT for token authentication
The next change direction of database - cloud native database
Servlet面试题
Global and Chinese market of centrifugal pumps 2022-2028: Research Report on technology, participants, trends, market size and share
DC/DC变换器轻载时三种工作模式的原理及优缺点
Are you a "social bull" or a "social terrorist" in the interview?
Data set preparation and arrangement
【十分钟】manim安装 2022
Deep learning: implementation skills of deep neural network
炒现货黄金的交易平台如何保障资金安全?
GTK interface programming (II): key components





![[live broadcast notes 0629] Concurrent Programming II: lock](/img/5c/42f5c9a9969b4d2bb950a7caac5555.png)


