委托
委托是一种数据类型
1.委托的用法示意:
//1.定义委托类型。
//委托是一种数据类型,可以存储方法。
//定义一个名字叫MyDelegate的委托类型,用来保存无参数,无返回值的方法。
public delegate void MyDelegate();
class Program
{
static void Main(string[] args)
{
//2. 声明委托变量,并赋值。
//声明了一个委托变量md,并且new了一个委托对象,并且把方法M1传递进去
//即md委托保存了M1方法。
MyDelegate md = new MyDelegate(M1);
//3. 调用md委托的时候,相当于调用了M1方法。
md();
Console.WriteLine("ok");
Console.ReadKey();
}
static void M1()
{
Console.WriteLine("没有参数没有返回值的方法");
}
}
2. 委托的意义示例:
定义一个类:
namespace 对象1
{
class TextClass1
{
//委托,将方法作为参数进行处理。
public void DoSomething(WriteTimeDelegate writeTime)
{
Console.WriteLine("========================");
Console.WriteLine("========================");
//执行完第二句代码时,输出一下系统时间
//Console.WriteLine(DateTime.Now.ToString());
//当程序执行到这一步之后,接着执行的就是委托传进来的函数。
if(writeTime!=null)
{
writeTime();
}
Console.WriteLine("========================");
Console.WriteLine("========================");
}
}
//定义一个委托,用来存储没有返回值,没有参数的方法
public delegate void WriteTimeDelegate();
}
实例化上面的类,并调用其中的函数,但是在函数中将自己写的另外的一个函数传递进该函数,实现代码的灵活性。
namespace 对象1
{
class Program
{
static void Main(string[] args)
{
TextClass1 text = new TextClass1();
text.DoSomething(PrintTimeToConsole);
Console.ReadKey();
}
//这个方法就是使用委托传进去的方法。
static void PrintTimeToConsole()
{
Console.WriteLine(DateTime.Now.ToString());
}
}
}
案例2: 带参数带返回值的委托类型。
首先定义一个类:
namespace 对象1
{
class TextClass1
{
//委托,将方法作为参数进行处理。
public void ChangeString(string[] strs,GetStringDelegate change)
{
for(int i=0;i<strs.Length;i++)
{
//此处执行委托的函数。
strs[i] = change(strs[i]);
}
}
}
//定义一个有参数,有返回值的委托
public delegate string GetStringDelegate(string name);
}
此处实现委托:
namespace 对象1
{
class Program
{
static void Main(string[] args)
{
//TextClass1 text = new TextClass1();
//text.DoSomething(PrintTimeToConsole);
TextClass1 text1 = new TextClass1();
string[] names = new string[] { "xxx", "sss", "aaaa" };
text1.ChangeString(names, ChangeString);
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
//带参数,带返回值的函数。(传入委托的参数)。
static string ChangeString(string name)
{
return name.ToUpper();
}
}
}
案例3:使用委托进行窗体之间的传值
窗体一的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 委托_窗体之间传值
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static public string text1;
private void button1_Click(object sender, EventArgs e)
{
text1 = textBox1.Text.ToString().Trim();
//想要在窗体二中改变窗体一中textbox的值,首先,窗体一中有一个函数,
//专门用来改变窗体一中textbox的值。因此在窗体二中执行这个函数就可以达到目的。
//将该函数作为一个参数,传递给窗体二,这样窗体二就可以执行该函数了。
Form2 from2 = new Form2(UpdataTextBox);
from2.Show();
}
public void Form1_Load(object sender, EventArgs e)
{
}
//该函数就是用来该表窗体1textbox中的值
private void UpdataTextBox(string val)
{
this.textBox1.Text = val;
}
}
//委托。将函数作为参数。
public delegate void UpdataTextDelegate(string text);
}
窗体二的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 委托_窗体之间传值
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(UpdataTextDelegate updataText)
:this()
{
//首先窗体二拿到窗体一传过来的函数参数
this._update = updataText;
}
private UpdataTextDelegate _update;
private void button1_Click(object sender, EventArgs e)
{
//将当前窗体中文本框中的值,赋值给窗体一
string value = this.textBox1.Text.Trim();
//执行拿到的改变窗体一中的值的函数,就可以达到目的。
this._update(textBox1.Text.Trim());
//关闭当前窗体
this.Close();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.text1;
}
}
}
3. Lambda 表达式
没有参数没有返回值的Lambda表达式的用法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 接口
{
class Program
{
static void Main(string[] args)
{
//没有参数,没有返回值的方法(使用Lambda表达式)
MyDelegate md = () =>
{
Console.WriteLine("lambda表达式");
};
md.Invoke();
Console.ReadKey();
}
}
public delegate void MyDelegate();
public delegate void Mydelegate1(string val);
public delegate int Mydelegate2(int n1,int n2,int n3);
}
有参数没有返回值的方法:
Lambda表达式不需要传递数据类型,因为委托已经限定了数据类型。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 接口
{
class Program
{
static void Main(string[] args)
{
//有参数,没有返回值的方法(使用Lambda表达式)
//此处m就是参数,m不用指定数据类型,应为该委托已经限定了参数必须是string类型。
Mydelegate1 md = m =>
{
Console.WriteLine(m);
};
md.Invoke("xsw");
Console.ReadKey();
}
}
public delegate void MyDelegate();
public delegate void Mydelegate1(string val);
public delegate int Mydelegate2(int n1,int n2,int n3);
}
有参数,有返回值的Lambda表达式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 接口
{
class Program
{
static void Main(string[] args)
{
//有多个参数,有返回值的方法(使用Lambda表达式)
Mydelegate2 md = (n1,n2,n3) =>
{
return n1 + n2 + n3;
};
int n4 = md.Invoke(1,2,3);
Console.WriteLine(n4);
Console.ReadKey();
}
}
public delegate void MyDelegate();
public delegate void Mydelegate1(string val);
public delegate int Mydelegate2(int n1,int n2,int n3);
}