c# 事件学习之笔记

事件的理解
  • 举例子说明事件,你正在家里吃饭,突然电话响了,你放下手中的饭碗去接电话。电话响就是事件的触发,也就是事件。
c#中事件的介绍
  • c#中的事件和委托是息息相关的,就是在定义委托变量的基础上加上关键字event。
  • 我认为在学习事件这部内容时,从事件触发的角度来理解事件可以容易掌握,以下在介绍事件中调用事件以触发事件来说明。
 public delegate void delegateName();  //先声明一个委托
 public event delegateName EventName; //声明一个事件
  1. 委托可以在命名空间下声明也可以在类空间下声明,对于委托一般都是在命名空间下声明
  2. 事件是声明要在类空间下声明的
  • 定义方法(定义的方法的签名需要和委托签名是一样的)
 static void method2()
        {
            Console.WriteLine("引用方法二 ");

        }
  • 事件引用方法及调用事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    public delegate void delegateNmae();
    public class Program
    {
        public event delegateName EventName;
        static void Main(string[] args)
        {
            Program p = new Program();
            p.EventName += method2;  //当事件触发时需要执行的方法
            p.EventName();     //触发事件
            Console.ReadKey();
        }
        
        //当触发事件执行这段代码
        static void method2()
        {
            Console.WriteLine("触发事件执行method2方法 ");

        }
  1. 需要注意的是:使用引用方法或者说关联方法只能使用+=运算号,取消引用使用-=运算符
为了更进一步从触发角度理解
  • 在class1类的for循环中,当i=5时触发事件,执行Program类中method2方法
  1. 创建一个类class1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    public delegate void delegateName();
    public class Class1
    {
        public event delegateName EventName;
        
        public void Method1()
        {
            for(int i=0; i<10; i++)
            {
                if(i ==5) //当i=5触发事件(事件触发的条件)
                {
                    EventName();   // 事件的触发
                    return;
                }
                Console.WriteLine(i);
            }
        }
    }
}
  1. 代码执行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    public class Program
    {
       static void Main(string[] args)
        {
            Class1 cl = new Class1();
            cl.EventName += method2;
            cl.Method1();
            Console.ReadKey();
        }

        static void method2()
        {
            Console.WriteLine("触发事件执行method2方法 ");

        }        
    }    
}
  1. 通过上述代码总结得出:委托声明的位置在命名空间下声明;事件的声明需要在那些类触发事件,我们那个类下进行声明;事件的和方法的关联一般要和方法定义在一个class空间下;需要在哪里触发就哪里调用事件
在事件中预定于的委托
  • 如果做winform开发的同学,我们给按键增加Click事件就发现,我们一直在想为什么添加了这段代码之后,我按下按键就可以执行后面的代码了呢?EventHandler到底是一个什么神奇的东西呢?
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
  • 通过元数据我们发现原来EventHandle这是一个编译器预定的委托而已,这下可以放心了
 public delegate void EventHandler(object sender, EventArgs e);
  1. 我们发现在EventHandle委托声明了两个参数,object sender:事件源 , EventArgs e 事件中包含的数据对象。
  2. 在EventHandler中不不含事件数据对象的。
在事件中事件中包含的数据对象

1.创建product类产生偶数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{

    public delegate void Event(object sender, int e);
    public class Product
    {
        public event Event EventEvent;
        public void Method1()
        {
            for(int i=0; i<10; i++)
            {
                if(i %2 == 0) 
                {
                    EventEvent(this, i);  //当偶数的时候触发事件,并将值传入事件方法
                   
                }
            }
        }
    }
}

  1. 执行代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApp3
{
    public class Program
    {
       static void Main(string[] args)
        {
            Product p = new Product();
            p.EventEvent += method;
            p.Method1();
            Console.ReadKey();
        }

        static void method(object sender, int i)
        {
            Console.WriteLine(i);
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值