您的位置:首页 > 编程语言 > C#

C#委托和事件

2013-11-21 14:45 99 查看
问题描述:

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}\n", 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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: