当前位置:网站首页>了解下C# 匿名方法
了解下C# 匿名方法
2022-07-31 21:54:00 【51CTO】
委托是用于引用与其具有相同标签的方法。换句话说,可以使用委托对象调用可由委托引用的方法。 |
匿名方法(Anonymous methods) 提供了一种传递代码块作为委托参数的技术。匿名方法是没有名称只有主体的方法。
在匿名方法中您不需要指定返回类型,它是从方法主体内的 return 语句推断的。
编写匿名方法的语法
匿名方法是通过使用 delegate 关键字创建委托实例来声明的。例如:
代码块 Console.WriteLine("Anonymous Method: {0}", x); 是匿名方法的主体。
委托可以通过匿名方法调用,也可以通过命名方法调用,即,通过向委托对象传递方法参数。
注意: 匿名方法的主体后面需要一个 ;。
例如:
实例
下面的实例演示了匿名方法的概念:
实例
当上面的代码被编译和执行时,它会产生下列结果:
边栏推荐
- 利用反射实现一个管理对象信息的简单框架
- BM5 merge k sorted linked lists
- 全网一触即发,自媒体人的内容分发全能助手——融媒宝
- multithreaded lock
- Memblaze released the first enterprise-grade SSD based on long-lasting particles. What is the new value behind it?
- linux查看redis版本命令(linux查看mysql版本号)
- A shortcut to search for specific character content in idea
- Flink_CDC construction and simple use
- Count characters in UTF-8 string function
- Architect 04 - Application Service Encryption Design and Practice
猜你喜欢
Socket回顾与I/0模型
顺序表的实现
Chapter VII
Shell script quick start to actual combat -02
What's wrong with the sql syntax in my sql
STM32 full series development firmware installation guide under Arduino framework
Introduction to Audio Types and Encoding Formats in Unity
Daily practice——Randomly generate an integer between 1-100 and see how many times you can guess.Requirements: The number of guesses cannot exceed 7 times, and after each guess, it will prompt "bigger"
How to identify fake reptiles?
Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
随机推荐
OSPFv3的基本配置
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios
Judging decimal points and rounding of decimal operations in Golang
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
-xms -xmx(information value)
linux查看redis版本命令(linux查看mysql版本号)
Introduction to Audio Types and Encoding Formats in Unity
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
全网一触即发,自媒体人的内容分发全能助手——融媒宝
Golang must know the Go Mod command
Flex layout in detail
Implementing a Simple Framework for Managing Object Information Using Reflection
BM5 merge k sorted linked lists
idea中搜索具体的字符内容的快捷方式
Shell script quick start to actual combat -02
C程序设计-方法与实践(清华大学出版社)习题解析
Several methods for deleting specified elements in Golang slices
【Yugong Series】July 2022 Go Teaching Course 025-Recursive Function
老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11