C# 委托、lambda表达式和事件
什么是委托?委托就是持有一个或多个方法的对象,并且该对象可以执行,可以传递。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
// 定义委托事件
delegate void ActCute();
static void Main(string[] args)
{
ActCute del = null;
Dog dog1 = new Dog("dog");
Cat cat1 = new Cat("cat");
// 委托事件的添加
del = dog1.WagTail;
del += cat1.InnocentLook;
// 定义lambda 表达式
del += () =>
{
Console.WriteLine("这是lambda表达式定义的方法!!!");
};
// 按照添加的顺序依次调用
del();
}
public class Dog
{
private string Name;
public Dog(string name)
{
Name = name;
}
public void WagTail()
{
Console.WriteLine(Name + "正在摇尾巴...");
}
}
public class Cat
{
private string Name;
public Cat(string name)
{
Name = name;
}
public void InnocentLook()
{
Console.WriteLine(Name + "正在摆弄无辜表情...");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication2
{
class Program
{
delegate void Func1();
// 使用方法
public static void Aminal()
{
Console.WriteLine("我是一只动物...");
}
public static void Plant()
{
Console.WriteLine("我是一棵植物...");
}
static void Main(string[] args)
{
Func1 myfunc1;
myfunc1 = new Func1(Aminal);
myfunc1 += new Func1(Plant);
myfunc1(); }
}
}
委托是一个类型,事件是委托的一个特殊实例。
定义一个委托实例是不完全的,可以被外部的类调用内部的委托;事件可以在外部定义,但是触发事件是在内部。所以相对于普通的委托,事件是安全的。
事件:一种封装受限制的委托。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Client1 c1 = new Client1();
Client1 c2 = new Client1();
// 绑定委托
Dog.NewDog += c1.WangADog;
Dog.NewDog += c2.WangADog;
Dog dog1 = new Dog("jack");
}
public class Dog
{
private string Name;
public delegate void Handler(); // 定义委托类型
public static event Handler NewDog; // 在委托事情上定义事件
public Dog(string name)
{
Name = name;
if (NewDog != null)
{
NewDog();
}
}
public void WagTail()
{
Console.WriteLine(Name + "正在摇尾巴...");
}
}
class Client1
{
public void WangADog()
{
Console.WriteLine("我想要一条狗!!!");
}
}
}
}
2019-7-29
委托和事件在窗体之间的应用:
委托:
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 ParentForm : Form
{
public Action<string> ChildFrm { get; set; } // 定义一个委托
public ParentForm()
{
InitializeComponent();
} private void ParentForm_Load(object sender, EventArgs e)
{
ChildForm frm = new ChildForm(); // 初始化子窗体
ChildFrm += frm.SetText; // 绑定事件
frm.Show();
} private void Btn_Send_Click(object sender, EventArgs e)
{
ChildFrm(this.TB_Msg.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 ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
} private void ChildForm_Load(object sender, EventArgs e)
{ }
public void SetText(string text)
{
this.TB_Msg.Text = 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 ParentForm : Form
{
public event EventHandler ChildFrmEvent; // 定义一个事件
public ParentForm()
{
InitializeComponent();
} private void ParentForm_Load(object sender, EventArgs e)
{
ChildForm frm = new ChildForm(); // 初始化子窗体
ChildFrmEvent += frm.FrmEvent; // 绑定子窗体里面的事件
frm.Show(); // 显示
} private void Btn_Send_Click(object sender, EventArgs e)
{
ChildFrmEvent(this, new NewEventArgsClass() { Text = this.TB_Msg.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 ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
} private void ChildForm_Load(object sender, EventArgs e)
{
}
public void FrmEvent(object sender, EventArgs e)
{
NewEventArgsClass args = e as NewEventArgsClass;
TB_Msg.Text = args.Text;
}
}
}
===================================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks
namespace 窗体之间的传参
{
class NewEventArgsClass:EventArgs
{
public string Text { get; set; }
}
}
管家解耦父子窗体(发布订阅模式的非委托实现):
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 MasterForm : Form
{
public MasterForm()
{
InitializeComponent();
} private void MasterForm_Load(object sender, EventArgs e)
{
// 启动父子窗体
ParentForm PFrm = new ParentForm();
ChildForm CFrm = new ChildForm();
PFrm.ChildList = new List<TotalInterface>();
PFrm.ChildList.Add(CFrm);
PFrm.Show();
CFrm.Show();
}
}
}
=======================================================================
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 ParentForm : Form
{
public List<TotalInterface> ChildList { get; set; }
public ParentForm()
{
InitializeComponent();
} private void ParentForm_Load(object sender, EventArgs e)
{ } private void Btn_Send_Click(object sender, EventArgs e)
{
if (ChildList == null) return;
foreach(TotalInterface item in ChildList)
{
item.SetText(this.TB_Msg.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 ChildForm : Form,TotalInterface
{
public ChildForm()
{
InitializeComponent();
} public void SetText(string text)
{
this.TB_Msg.Text = text;
} private void ChildForm_Load(object sender, EventArgs e)
{ }
}
}
=============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 管家解耦父子窗体
{
// 定义接口
public interface TotalInterface
{
void SetText(string text);
}
}
C# 委托、lambda表达式和事件的更多相关文章
- C#编程 委托 Lambda表达式和事件
委托 如果我们要把方法当做参数来传递的话,就要用到委托.简单来说委托是一个类型,这个类型可以赋值一个方法的引用. 声明委托 在C#中使用一个类分两个阶段,首选定义这个类,告诉编译器这个类由什么字段和方 ...
- C#学习笔记三(委托·lambda表达式和事件,字符串和正则表达式,集合,特殊的集合)
委托和事件的区别 序号 区别 委托 事件 1 是否可以使用=来赋值 是 否 2 是否可以在类外部进行调用 是 否 3 是否是一个类型 是 否,事件修饰的是一个对象 public delegate vo ...
- 委托、Lambda表达式、事件系列07,使用EventHandler委托
谈到事件注册,EventHandler是最常用的. EventHandler是一个委托,接收2个形参.sender是指事件的发起者,e代表事件参数. □ 使用EventHandler实现猜拳游戏 使用 ...
- 委托、Lambda表达式、事件系列06,使用Action实现观察者模式,体验委托和事件的区别
在"实现观察者模式(Observer Pattern)的2种方式"中,曾经通过接口的方式.委托与事件的方式实现过观察者模式.本篇体验使用Action实现此模式,并从中体验委托与事件 ...
- 委托、Lambda表达式、事件系列05,Action委托与闭包
来看使用Action委托的一个实例: static void Main(string[] args) { int i = 0; Action a = () => i++; a(); a(); C ...
- 委托、Lambda表达式、事件系列04,委托链是怎样形成的, 多播委托, 调用委托链方法,委托链异常处理
委托是多播委托,我们可以通过"+="把多个方法赋给委托变量,这样就形成了一个委托链.本篇的话题包括:委托链是怎样形成的,如何调用委托链方法,以及委托链异常处理. □ 调用返回类型为 ...
- 委托、Lambda表达式、事件系列03,从委托到Lamda表达式
在"委托.Lambda表达式.事件系列02,什么时候该用委托"一文中,使用委托让代码简洁了不少. namespace ConsoleApplication2 { internal ...
- 委托、Lambda表达式、事件系列02,什么时候该用委托
假设要找出整型集合中小于5的数. static void Main(string[] args) { IEnumerable<int> source = new List<int&g ...
- 委托、Lambda表达式、事件系列01,委托是什么,委托的基本用法,委托的Method和Target属性
委托是一个类. namespace ConsoleApplication1 { internal delegate void MyDelegate(int val); class Program { ...
- C#高级编程(第9版) 第08章 委托、lambda表达式和事件 笔记
本章代码分为以下几个主要的示例文件: 1. 简单委托 2. 冒泡排序 3. lambda表达式 4. 事件示例 5. 弱事件 引用方法 委托是寻址方法的.NET版本.在C++中函数 ...
随机推荐
- MySQL:数据类型介绍
数据类型介绍 一.整数类型(可以添加自增约束条件) 数据类型 存储需要 有符号 无符号 tinyint 1个字节 -2^7~2^7-1 0~2^8 smallint 2个字节 -2^15~2^15-1 ...
- django面试题必知
Django的Model的继承有几种形式,分别是什么?(私信小编001 .002 .003 .007任何一个即可获取Python学习资料) 一.抽象继承: 这种继承的定义方法如下: 上例中,我们的Hu ...
- idea本地安装 lombok插件
转:https://blog.csdn.net/weixin_41404773/article/details/80689639 idea本地安装 lombok插件 项目中经常使用bean,entit ...
- Oracle自我补充之OVER()函数介绍
OVER(PARTITION BY)函数介绍 开窗函数 Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返 ...
- AE旋转
精准对位: 好几个图层上的旋转点在一个位置上: 方法1:勾选网格,定点. 方法2:按住ctrl+r 调出尺寸.拖参考线,焦点自动吸附功能. 选中四张或者选中第一张,按shift键,选中最后一张(即可 ...
- 修改select样式
CSS就可以解决,原理是将浏览器默认的下拉框样式清除,然后应用上自己的,再附一张向右对齐小箭头的图片即可. select { /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ ...
- 20155208徐子涵 2016-2017-2 《Java程序设计》第5周学习总结
20155208徐子涵 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承结构 Java中所有错误都会被打包为对象,运用tr ...
- 牛客国庆集训派对Day4 J-寻找复读机
链接:https://www.nowcoder.com/acm/contest/204/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- php-fpm的pool池子、php慢日志记录、open_basedir、php-fpm进程管理
1.php-fpm的poo池子:目的:可以让不同的网站,对于不同的php解析,可以把不同的网站解析区分开.编辑:vim /usr/local/php5-fpm/etc/php-fpm.conf加入: ...
- C++学习(二十)(C语言部分)之 函数1
函数 printf 输出的函数 scanf 输入的函数函数是什么 怎么写 是一组一起执行一个任务的语句 一个程序的基本组成单位是函数 只需要知道函数名字和括号里面要填的内容 就可以调用函数 1.如果代 ...