C#_委托的使用
来源:互联网 发布:晋城监狱网络改造 编辑:程序博客网 时间:2024/05/02 04:23
// 委托其实就是一种数据类型 delegate void behaviourDelegate(); class Person { public void sayDelegate() { Console.WriteLine("sayDelegate"); } public void talkDelegate() { Console.WriteLine("talkDelegate"); } public void walkDelegate() { Console.WriteLine("walkDelegate"); } } class Start { static void Main(string[] args) { var per = new Person(); // 初始化单一委托 behaviourDelegate del1 = new behaviourDelegate(per.sayDelegate); del1(); // 初始化多播委托 behaviourDelegate del2 = null; del2 += per.sayDelegate; del2 += per.talkDelegate; del2 += per.walkDelegate; del2(); Console.ReadLine(); } }
using System;using System.Collections;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;namespace CShapeTest{ class Start { // 没有参数,没有返回值 static void PrintStr() { Console.WriteLine("PrintStr"); } // 有一个参数或者多个参数,没有返回值 static void PrintInt(int a, int b) { Console.WriteLine(a + b); } static void Main(string[] args) { // 系统内置委托类型Action //Action a = new Action(PrintStr); //a();//常用调用方式 //a.Invoke();//不常用调用方式 // Action最多只能有16个参数 Action<int, int> a = new Action<int, int>(PrintInt); a(1, 2); Console.ReadLine(); } }}
using System;using System.Collections;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;namespace CShapeTest{ class Start { // 没有参数,返回值 static string PrintStr() { return "Hello world"; } // 有一个参数或者多个参数,有返回值 static int PrintInt(int a, int b) { return a + b; } static void Main(string[] args) { // 系统内置委托类型Func,Func一定有返回值 //Func<string> func = new Func<string>(PrintStr); //Console.WriteLine(func()); Func<int, int, int> func = new Func<int, int, int>(PrintInt); Console.WriteLine(func(1, 5)); Console.ReadLine(); } }}
0 0
- C#_委托的使用
- C#_委托
- C#_事件_委托
- 黑马程序员_委托的使用
- c#_[委托]的个人详解,定义与演示。
- 委托的的使用举例[C#]
- 委托的定义_委托的实例
- C#_事件的使用
- Objective-c语言_委托模式
- C#委托-委托的使用
- [MinGW]_[C/C++]_[msys的使用问题]
- 通俗易懂讲解委托的使用妙文(C#)
- 【C#】C#委托和字典的结合使用
- C#_关键字params的使用
- C++_内置函数的使用
- C++_函数的重载使用举例
- C++_类模板的使用
- [Objective-c]_[NSString的基本使用]
- [Oracle] 逻辑结构实验与总结
- C/C++程序员学习路线
- H - Flip Game poj 1753
- 【Unity3D游戏开发】Application.systemLanguage无法区分简体中文和繁体中文 (二六)
- Git 项目上传至github入门实战并解决常见错误
- C#_委托的使用
- Android Studio中运行项目出现 java.exe'' finished with non-zero exit value 2
- 9.2 Zynq嵌入式系统调试方法
- Java(8)--初识反射
- CROS
- B/S 开发和 C/S开发的区别
- arm 汇编语言编写开始部分
- Android Service通信
- CSS3之边框属性border