当前位置:网站首页>C anonymous function
C anonymous function
2022-06-09 06:01:00 【Go_ Accepted】
1、 Anonymous functions
Functions without names , It is mainly used in conjunction with delegation and events , Anonymous functions are not used without delegates and events
2、 Basic grammar
delegate ( parameter list ){ // Functional logic };When to use ?
(1) When passing delegate parameters in a function
(2) Delegate and event assignment
3、 Use
(1) No parameter no return
Action a = delegate () { Console.WriteLine(" Anonymous functions "); }; a();(2) Ginseng
Action<int> a = delegate (int value) { Console.WriteLine(value); }; a(100);(3) There is a return value
direct return, Automatic recognition of return value
Func<string> a = delegate () { return " Return value anonymous function "; }; Console.WriteLine(a());(4) In general, it will be passed as a function parameter , Or as a function return value
class Test { public Action action; // Pass as a parameter public void DoSomeThing(int a, Action action) { Console.WriteLine(a); action(); } // As return value public Action GetFun() { return TestRet; } public void TestRet() { } // Or in one step public Action GetFun2() { return delegate () { Console.WriteLine(" Anonymous function as return value "); }; } }A call passed as a parameter
Test t = new Test(); // Mode one t.DoSomeThing(100, delegate () { Console.WriteLine(" Anonymous functions passed as arguments "); }); // Mode two Action ac = delegate () { Console.WriteLine(" Anonymous functions passed as arguments "); }; t.DoSomeThing(100, ac);Call as return value
// Mode one Action ac = t.GetFun2(); ac(); // Mode two t.GetFun2()();
4、 Disadvantages of anonymous functions
After adding to the delegate or event container , Cannot be removed separately , Can only be emptied directly
Be careful !!! Anonymous functions change the life cycle of variables , for example :
static Func<int, int> TestFun(int v) { return delegate (int i) { return i * v; }; }And then call
Func<int, int> fun = TestFun(2); Console.WriteLine(fun(3))You can find ,v The life cycle of TestFun() It doesn't stop at the end
边栏推荐
- [paper] cbam: revolutionary block attention module
- 复杂流程挖掘技术隐藏于平民化产品背后-专访容智信息创始人柴亚团
- A guide to signing adobe air applications using code signing certificates
- C# Lambda表达式
- XML建模
- Educational Codeforces Round 20 E. Roma and Poker
- Easyexcel export, self encapsulating header and sequence
- Use the default value method if the configuration file does not exist
- Oracle lock table solution
- 【2022初春】【LeetCode】45. 跳跃游戏 II
猜你喜欢

【论文】CBAM: Convolutional Block Attention Module

Interpretation of join method in thread

Go language tutorial 02 go common libraries + compile consult

线程 interrupted 详细解析

NAND flash Basics

Allocation principle of IP address

Jitsi meet video recording with jibri

Ffmpeg pulls webrtc streams, and the first all open source solution in the metartc industry is coming

DBeaver导出查询数据sql文件

iTOP-2K1000开发板启动ramdisk-制作启动U盘
随机推荐
复杂流程挖掘技术隐藏于平民化产品背后-专访容智信息创始人柴亚团
【2022初春】【LeetCode】45. 跳跃游戏 II
el-table滚动条样式
Use the default value method if the configuration file does not exist
Creating a digital elevation model (DEM) using point cloud data
C# 事件
VK Cup 2017 - Round 3 B. Dynamic Problem Scoring
Analysis of the most complete webrtc server technology selection in history
Record TCP time once_ Blood cases caused by wait
VK Cup 2017 - Round 2 A. Voltage Keepsake
Embedded audio and video solutions webrtc vs metartc
srs-nodejs
代码签名证书的时间戳验证码签名方法
Debian11 fix the port number after installing NFS server to set firewall
Jdbc-dbutils
New and old versions of Minio tool classes and deployment documents
ThreadLocal parsing
NAND flash Basics
MVCC多版本控制
Offline data synchronization platform datax+ report visualization platform metabase