委托 delegate

public delegate string MyDelegate(int num1);  //定义委托类型
class Program{ 
static void Main(string[] args){ 

  int t1 = Test1(Show);

 Program1 p1 = new Program1();
int t2 = Test1(p1.Display);
} 

 

public static string Show(int num){ 

  return "人数为:"+ num;

}

public static int Test1(MyDelegate myDel){

  string str = myDel(3);

   return str.Length;

}

}


public class Program1{
  public string Display(int a){
  return "车辆数为:"+ a;
  }
}

 

 

实现加减乘除

加减乘除时 统一调用方法 calc

public delegate int MyDelegate(int a, int b);

int
calc(int a, int b, MyDeletgate operation){
  return operation(a,b); 
}

main(){
  int c1 = calc(3,1,add);
  int c2 = calc(3,2,minus);  
  ...
}

 

 int add(int a, int b){

  return a+b;

}

int minus(int a, int b){

  return a-b;

}

int plus(int a ,int b ){

  return a*b;

}

 

int divide(int a, int b){ 

  return a/b;

}

public delegate void MyDelegate();

public delegate int MyDelegateCalc(int a , int b);
class Program 
 { 
  static void Main(string[] args) 
{ 
MyClass mc = new MyClass(); 

 DoSth(mc.DoTime);

//DoSth(WriteTime);
DoCalc((a,b)=> a+b);
DoCalc((a,b)=> a-b);
Console.ReadKey();
} 
  public static void DoSth(MyDelegate mdl) //将方法作为参数传递 
{ 
  Console.WriteLine("吃早饭"); 
  Console.WriteLine("读书"); 
  Console.WriteLine("飞流直下三千尺"); 
  Console.WriteLine("疑是银河落九天"); 
  //做事 不知道做什么
  mdl(); 
  // Console.WriteLine(System.DateTime.Now.ToString()); 
//File.WriteAllText("1.txt",System.DateTime.Now.ToString()); 
  Console.WriteLine("吃饭"); 
} 
public static void DoCalc(MyDelegateCalc mdl) 
{ 
  int c = mdl(3, 4); 
} 
public static void ShowTime() { Console.WriteLine(System.DateTime.Now.ToString()); } 

public static void WriteTime() { File.WriteAllText("1.txt",System.DateTime.Now.ToString()); }

笔记:

MyDelegate是委托 , 委托是一种特殊的类型 ,委托的实例为一个方法。委托 可以把方法作为参数传递。

 

 

 

public delegate void MyDelegate(); 
public delegate void MyDel(int a); 
public delegate int MyDelAdd(int a, int b);
public delegate void MyDelWrite(string name);
public delegate string MyDelWrite2(string name);
//匿名方法 
MyDelegate mdl = delegate(){ 
  Console.WriteLine("aaeew"); 
};
等价于
拉姆达表达式
MyDelegate mdl = ()=>{
  Console.WriteLine("aaeew");
};

MyDelWrite mdlWrite = x => Console.WriteLine(x);
mdlWrite("iae32a");

MyDelWrite2 mdlWrite2 = x=> x+"好帅";
string str = mdlWrite2("小黑");

MyDelegate mdlPrint = print; //可以用现有方法赋值
等价于
MyDelegate mdlPrint = new MyDelegate(print);
MyDel mdl2 = delegate(int a){ 
  Console.WriteLine(a+10); 
};
MyDelAdd mdl3 = delegate(int a, int b){
  return a+b;
};
类似写法
MyDelAdd mdlAdd = (int a, int b)=>{
  return a+b;
}
//使用匿名方法 
mdl(); 
mdl2(3);
int c = mdl3(122,9);
int c2 = mdlAdd(9,8);

mdlPrint();

void print(){
  //...
}
List<int> list = new List<int>() {3,4,5,6,7,8,9,10 }; 
IEnumerable<int> number= list.Where(x => x > 5); 
foreach (int item in number) 
{ 
   Console.WriteLine(item); 
} 

 

#region 代码段

#region MyRegion 
private ApplicationSignInManager _signInManager; 
private ApplicationUserManager _userManager; 
#endregion 

 

 

 

多播委托

  //public delegate void MyDelegate();
public delegate int MyDelegate();

Action 和 Func 都是系统提供的委托类,有了这两个类之后,一般就不用自己写 自定义的委托类了
//Action<int> ac = new Action<int>(); 有参数无返回值
  //ac(10); 
  //Func<int> fc = new Func<int>(); 有返回值,最后一个泛型参数为返回值,此处int为返回值类型 
  //Func<int,string,string>fc=new Func<int,string,string>(  int,string为参数类型,string为返回值类型 
  //MyDelegate mdl = T1; 多播委托 
  //mdl += T2; 叠加 
  //mdl += T3; 
  //mdl += T4; 
  //mdl(); 按照 += 的顺序依次执行 
  //Console.ReadKey(); 
  //委托变量赋值方法后 ,在调用这个委托变量的时候 要先判断该委托变量不为null 才能调用
// if(mdl!=null){ mdl(); }

 

  MyDelegate mdl = T1; 
  mdl += T2; 
  mdl += T3; 
  mdl += T4; 

 

  mdl-= T2; //也可减 
  if (mdl!=null) 
  {

  int result = mdl(); //返回值为最后一个函数的返回值
  Console.WriteLine(result); 
  Console.ReadKey(); 
} 
} 
public statit int T1() 
{ 
  return 1; 
} 
public static int T2() 
{    return 2; 
} 

public static int T3()
{ 
  return 3; 
} 
public static int T4() 
{   return 4; 
} 
//public static void T1() 
//{ 
// Console.WriteLine("1"); 
//} 
//public static void T2() 
//{ 
// Console.WriteLine("2"); 
//} 
//public static void T3() 
//{ 
// Console.WriteLine("3"); 
//} 
//public static void T4() 
//{ 
// Console.WriteLine("4"); 
//} 

 

 

委托小案例——winform窗体之间传值

需求

publicpartialclass Form1 : Form 
 { 
 public Form1() 
{ 
  InitializeComponent(); 
} 

 

 private void btn1_Click(object sender, EventArgs e) 
{ 
 string t = txt1.Text;//获取第一个文本框的值
  Form2 f2 = new Form2(t,SetText); 
 // f2.Show();
  f2.Show(); 
} 
 public void SetText(string str) 
{ 
  txt1.Text = str;//把一个字符串变量的值赋值给第一个文本框 
} 
 } 
 public delegate void MyDelegte(string msg); 
 public partial class Form2 : Form 
 { 

 

 public Form2() 
{ 
  InitializeComponent(); 
} 
 public MyDelegte _mdl; 
public Form2(string str,MyDelegte mdl):this() 
{ 
 //str 是窗体1传过来的值,直接赋值给第二个窗体的文本框 
txt2.Text = str; 
this._mdl = mdl;//存起来了  
} 

 

private void btn2_Click(object sender, EventArgs e) 
{  if (this._mdl!=null) 
 {

  this._mdl(txt2.Text);
  this.Close();//关闭窗体 
} 
//txt2.Text 
} 
 } 

泛型委托 Func<> Action<T>

软件 LINQPad 4, 练习lambda表达式

 

泛型的逆变与协变

逆变和协变实际上都是把子类赋值给父类。

转载于:https://www.cnblogs.com/rockywood/p/6411851.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值