C#委托基础

1|0介绍







2|0使用







3|0普通委托详解

static void Main(string[] args) { int x = 100; int y = 300; //Get the pointer of the function "ToStirng" and apply it to the varible getAString GetAString getAString = new GetAString(x.ToString); GetAString getAString2 = x.ToString;//2nd Way to define: same as above Console.WriteLine(getAString());//Usable while calling by varible "getAString" Console.WriteLine(getAString2());//Usable while calling by varible "getAString2" string str = getAString.Invoke();//2nd way to call string str2 = getAString2.Invoke();//2nd way to call Console.WriteLine(str); Console.WriteLine(str2); MethodHolder myMethod = new MethodHolder(y.ToString); Console.WriteLine(AnotherMethod(myMethod)); } //Signature of the delegate delegate void VoidFuncWithOneParameter(int parameter); delegate int IntFuncWithOneParameter(int parameter); delegate int IntFuncWithTwoParameters(int parameterInt, string parameterStr); delegate string GetAString();//This will be treated as a class /*!IMPORTANT!*/ delegate string MethodHolder(); //delegate can be used to send a method as parameter to another method static string AnotherMethod(MethodHolder method) { return method();//just like a callback-function in JS }









4|0Action委托详解

static void Main(string[] args) { //Action is a delegate which points to a void method without parameters Action voidMethod = Method; voidMethod(); //Action<type of parameter> can points to a void method with parameters Action<int> voidMethodWithParameters = MethodWithParameters; voidMethodWithParameters(5); } static void Method() { Console.WriteLine("This is a method which returns nothing."); } static void MethodWithParameters(int parameter) { if (parameter > 0) { for (int i = 0; i < parameter; i++) { Console.WriteLine("This is a void method with a parameter."); } } else { Console.WriteLine("Parameter should be greater than 0"); } }






5|0Func委托详解

1|0与Action委托不同的是,Func委托可以代理具有返回值的方法。

static void Main(string[] args) { //The last parameter in "Func" is the type of return-value the method //while the former parameters are all the parameters' type of the target method Func<string, int, bool, float> matureMethod = MatureMethod; matureMethod("Hello world!", 6, true); //The single parameter means the return-value of the method Func<bool> simpleMethod = SimpleMethod; Console.WriteLine(simpleMethod()); } static float MatureMethod(string parameterStr, int parameterInt, bool parameterBool) { if (parameterBool && parameterInt > 0) { for (int i = 0; i < parameterInt; i++) { Console.WriteLine($"{i} : {parameterStr}"); } return 0.618f; } else { Console.WriteLine("3rd parameter is set to false or the 2nd parameter is smaller than 0"); return 3.14f; } } static bool SimpleMethod() { return true; }






6|0多播委托










作者:艾孜尔江;转载或使用请标明出处。


__EOF__

本文作者艾孜尔江
本文链接https://www.cnblogs.com/ezhar/p/12862724.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   艾孜尔江  阅读(157)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示