c#-委托
2015-07-07 18:01
375 查看
c#委托
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。
委托就是相当于c++中的指针。
总的来说委托就是方法里面还有方法!
步骤:1.定义一个委托,类型和参数; 2.用于委托的方法,类型和参数与委托保持一致; 3.调用委托; 实例一: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegateDemo
{
public delegate void callDelegate(int a, int b);
class Program
{
//定义一个委托
private static void big(int a, int b)
{
if (a > b)
Console.WriteLine("a"+a);
else
Console.WriteLine("a"+b);
}
private static void small(int a, int b)
{
if (a < b)
Console.WriteLine(a);
else
Console.WriteLine(b);
}
//在函数内使用委托
private static void Process(int a, int b, callDelegate call)
{
call(a, b);
}
static void Main(string[] args)
{
//委托实例与方法相关联
Process(10, 20,big);
Process(10, 20,small);
Console.ReadKey();
}
}
}
namespace delegateDemo { public class ab { //用于委托调用的方法 public int Big(int a, int b) { if (a > b) return a; else return b; } public int Small(int a, int b) { if (a < b) return a; else return b; } } class Program { //定义一个委托 public delegate int callDelegate( int a, int b);
//在函数内使用委托 public static int Process( int a, int b, callDelegate call) { return call(a, b); } static void Main(string[] args) { //委托实例与方法相关联 callDelegate delegateBig=new callDelegate( new ab().Big); int big = delegateBig(10, 20);
//用委托调用相关联的方法 int small = Process(10, 20, new callDelegate( new ab ().Small)); Console.WriteLine(big); Console.WriteLine(small); } } }
阅读更多
步骤:1.定义一个委托,类型和参数; 2.用于委托的方法,类型和参数与委托保持一致; 3.调用委托; 实例一: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delegateDemo
{
public delegate void callDelegate(int a, int b);
class Program
{
//定义一个委托
private static void big(int a, int b)
{
if (a > b)
Console.WriteLine("a"+a);
else
Console.WriteLine("a"+b);
}
private static void small(int a, int b)
{
if (a < b)
Console.WriteLine(a);
else
Console.WriteLine(b);
}
//在函数内使用委托
private static void Process(int a, int b, callDelegate call)
{
call(a, b);
}
static void Main(string[] args)
{
//委托实例与方法相关联
Process(10, 20,big);
Process(10, 20,small);
Console.ReadKey();
}
}
}
实例二:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace delegateDemo { public class ab { //用于委托调用的方法 public int Big(int a, int b) { if (a > b) return a; else return b; } public int Small(int a, int b) { if (a < b) return a; else return b; } } class Program { //定义一个委托 public delegate int callDelegate( int a, int b);
//在函数内使用委托 public static int Process( int a, int b, callDelegate call) { return call(a, b); } static void Main(string[] args) { //委托实例与方法相关联 callDelegate delegateBig=new callDelegate( new ab().Big); int big = delegateBig(10, 20);
//用委托调用相关联的方法 int small = Process(10, 20, new callDelegate( new ab ().Small)); Console.WriteLine(big); Console.WriteLine(small); } } }
阅读更多
相关文章推荐
- 事件与委托要点
- 浅谈IOS开发中委托(Delegate)的使用
- 大白话系列之C#委托与事件讲解(三)
- C# 委托的理解
- Qt自定义委托在QTableView中绘制控件、图片、文字
- 委托 (代码复习)
- C#异步委托之委托使用的时机浅析
- 关于Files Owner 和委托连接
- 关于iOS 委托 代理 协议 的理解
- 理解C++实现委托原理
- 匿名方法就是委托
- C#委托的介绍(delegate、Action、Func、predicate)
- 委托和事件的应用,模拟一个战斗机防盗器
- UITableView SDK委托方法详解
- C# -- 委托的介绍(delegate、Action、Func、predicate)
- .Net的异步机制(委托Delegate) - STEP 1
- 委托和线程
- 简单的C++委托 —— 用模板类实现类成员函数的回调
- 深入解析C# 委托事件和Lambda表达式
- 委托、匿名委托和lambda表达式