zoukankan      html  css  js  c++  java
  • C#委托和事件

    问题描述:

            C#委托和事件

    名词解释:

    委托概述


    委托具有以下特点:

    • 委托类似于 C++ 函数指针,但它们是类型安全的。

    • 委托允许将方法作为参数进行传递。

    • 委托可用于定义回调方法。

    • 委托可以链接在一起;例如,可以对一个事件调用多个方法。

    • 方法不必与委托签名完全匹配。

    C#
    public delegate int PerformCalculation(int x, int y);

    事件概述


    或对象可以通过事件向其他类或对象通知发生的相关事情。发送(或引发)事件的类称为“发行者”,接收(或处理)事件的类称为“订户”。

    事件具有以下特点:

    • 发行者确定何时引发事件,订户确定执行何种操作来响应该事件。

    • 一个事件可以有多个订户。一个订户可处理来自多个发行者的多个事件。

    • 没有订户的事件永远也不会引发。

    • 事件通常用于通知用户操作,例如,图形用户界面中的按钮单击或菜单选择操作。

    • 如果一个事件有多个订户,当引发该事件时,会同步调用多个事件处理程序。要异步调用事件,请参见使用异步方式调用同步方法

    • 可以利用事件同步线程。

    • 在 .NET Framework 类库中,事件是基于 EventHandler 委托和 EventArgs 基类的。

    具体使用:

    • 1. 委托
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace Delegate
    {
        public struct Book
        {
            public String title;
            public String author;
            public double price;
            public bool paperback;
    
            public Book(String title, String author, double price, bool paperback)
            {
                this.title = title;
                this.author = author;
                this.price = price;
                this.paperback = paperback;
            }
        }
    
        //声明委托
        public delegate void BookDelegate(Book book);
    
        class BookDB
        {
            private ArrayList list = new ArrayList();
    
            public void AddBook(Book book)
            {
                list.Add(book);
            }
    
            public void ProcessDelegate(BookDelegate dele)
            {
                foreach (Book elem in list)
                {
                    if (elem.paperback)
                    {
                        dele(elem); //调用外部的方法
                    }
                }
            }
        }
    }
    •  1.1 处理函数
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Delegate
    {
        public class TotalPrice
        {
            private int countBook = 0;
            private decimal booksPrice = 0.0m;
             
    
            internal void AddPrice(Book book)
            {
                this.countBook++;
                this.booksPrice += (decimal)book.price;
            }
    
            internal decimal AverPrice()
            {
                return booksPrice / countBook;
            }
        }
    }
    • 2. 事件
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Delegate
    {
        public class SampleEventArgs
        {
            public SampleEventArgs(string s) { Text = s; }
            public String Text { get; private set; } // readonly
        }
    
        public class Publisher
        {
            // Declare the delegate (if using non-generic pattern).
            public delegate void SampleEventHandler(object sender, SampleEventArgs e);
    
            // Declare the event.
            public event SampleEventHandler SampleEvent;
    
            public Publisher(SampleEventHandler handle)
            {
                SampleEvent=handle;
            }
            
            public void RaiseSampleEvent()
            {
                // Raise the event by using the () operator.
                SampleEvent(this, new SampleEventArgs("Hello"));
            }
    
            public void SayHi()
            {
                Console.WriteLine("hello world!");
            }
        }
    
    }
    • 3. 主函数:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Delegate
    {
        class Program
        {
            public static void PrintPrice(Book book)
            {
                Console.WriteLine(book.price);
            }
    
            static void Main(string[] args)
            {
                BookDB bookdb = new BookDB();
                TotalPrice total = new TotalPrice();
    
                for (int i = 0; i < 10; i++)
                {
                    Book book = new Book("Book" + i.ToString(), "songchao luo", 100.0 + i, true);
                    bookdb.AddBook(book);
                    bookdb.ProcessDelegate(total.AddPrice); //使用委托
                }
    
                //使用委托
                bookdb.ProcessDelegate(PrintPrice);
                Console.WriteLine("Average Price:{0}
    ", total.AverPrice());
    
    
                //事件
                Publisher pub = new Publisher(Occur);
                pub.RaiseSampleEvent();
            }
    
            //事件处理函数
            public static void Occur(object e, SampleEventArgs args)
            {
                Publisher newpub = (Publisher)e;
                newpub.SayHi();
                Console.WriteLine(args.Text);
            }
        }
    }
  • 相关阅读:
    C# zip压缩文件的功能
    C#图解教程 第四章 第五章
    process.StandardOutput.ReadToEnd() 假死
    C# 图解教程 (类型 存储和变量)
    unity editor下选中GameObject粘贴复制pos信息
    Texture中指定具体颜色进行高亮显示
    unity 加载Assetbundle文件夹路径需要注意
    unity 文件移动注意 AB打包文件名注意小写
    linux总结应用之三 建立内核
    linux总结应用之二
  • 原文地址:https://www.cnblogs.com/luosongchao/p/3435559.html
Copyright © 2011-2022 走看看