ASP.NET深入浅出系列4- 也谈委托和事件
2008-09-15 23:40
337 查看
这个话题已经很多人谈过了,本不想重复拿出来说,但为了这个系列的完整性,我便把自己以前积累的东西整理了一下,大家全当消遣了。
作者:方明
一、委托的定义:
《高级汉语大词典》中是如下解释的:托付给别的人或机构办理。要说生活中的意思其实大家都能理解,无非是“当某人(机构)需要完成一件自己不能或不应该完成的事情的时候,此人(机构)物色一个合适的且有能力完成此事的人选,然后提供必要的信息,将此事委托给物色到的人(机构)来完成。” C#中的委托是一种引用方法的类型,一旦为委托分配了方法,委托将与该方法具有完全相同的行为,委托方法的使用可以像其他任何方法一样具有参数和返回值。委托对象能被传递给调用该方法引用的代码而无须知道哪个方法将在编译时被调用。委托是函数的封装,它代表一“类”函数。他们都符合一定的签名:拥有相同的参数列表、返回值类型。同时委托也可以看作是对函数的抽象,是函数的“类”。此时,委托实例代表一个具体的函数。委托应该和类同属一个层面,使用起来也很象一个类。我们先来看一个委托使用的实例:
Code
// This example demonstrates the EventHandler<T> delegate.
using System;
using System.Collections.Generic;
//---------------------------------------------------------
public class MyEventArgs : EventArgs
{
private string msg;
public MyEventArgs( string messageData ) {
msg = messageData;
}
public string Message {
get { return msg; }
set { msg = value; }
}
}
//---------------------------------------------------------
public class HasEvent
{
// Declare an event of delegate type EventHandler of
// MyEventArgs.
public event EventHandler<MyEventArgs> SampleEvent;
public void DemoEvent(string val)
{
// Copy to a temporary variable to be thread-safe.
EventHandler<MyEventArgs> temp = SampleEvent;
if (temp != null)
temp(this, new MyEventArgs(val));
}
}
//---------------------------------------------------------
public class Sample
{
public static void Main()
{
HasEvent he = new HasEvent();
he.SampleEvent +=
new EventHandler<MyEventArgs>(SampleEventHandler);
he.DemoEvent("Hey there, Bruce!");
he.DemoEvent("How are you today?");
he.DemoEvent("I'm pretty good.");
he.DemoEvent("Thanks for asking!");
}
private static void SampleEventHandler(object src, MyEventArgs mea)
{
Console.WriteLine(mea.Message);
}
}
//---------------------------------------------------------
/*
This example produces the following results:
Hey there, Bruce!
How are you today?
I'm pretty good.
Thanks for asking!
*/
作者:方明
一、委托的定义:
《高级汉语大词典》中是如下解释的:托付给别的人或机构办理。要说生活中的意思其实大家都能理解,无非是“当某人(机构)需要完成一件自己不能或不应该完成的事情的时候,此人(机构)物色一个合适的且有能力完成此事的人选,然后提供必要的信息,将此事委托给物色到的人(机构)来完成。” C#中的委托是一种引用方法的类型,一旦为委托分配了方法,委托将与该方法具有完全相同的行为,委托方法的使用可以像其他任何方法一样具有参数和返回值。委托对象能被传递给调用该方法引用的代码而无须知道哪个方法将在编译时被调用。委托是函数的封装,它代表一“类”函数。他们都符合一定的签名:拥有相同的参数列表、返回值类型。同时委托也可以看作是对函数的抽象,是函数的“类”。此时,委托实例代表一个具体的函数。委托应该和类同属一个层面,使用起来也很象一个类。我们先来看一个委托使用的实例:
Code
// This example demonstrates the EventHandler<T> delegate.
using System;
using System.Collections.Generic;
//---------------------------------------------------------
public class MyEventArgs : EventArgs
{
private string msg;
public MyEventArgs( string messageData ) {
msg = messageData;
}
public string Message {
get { return msg; }
set { msg = value; }
}
}
//---------------------------------------------------------
public class HasEvent
{
// Declare an event of delegate type EventHandler of
// MyEventArgs.
public event EventHandler<MyEventArgs> SampleEvent;
public void DemoEvent(string val)
{
// Copy to a temporary variable to be thread-safe.
EventHandler<MyEventArgs> temp = SampleEvent;
if (temp != null)
temp(this, new MyEventArgs(val));
}
}
//---------------------------------------------------------
public class Sample
{
public static void Main()
{
HasEvent he = new HasEvent();
he.SampleEvent +=
new EventHandler<MyEventArgs>(SampleEventHandler);
he.DemoEvent("Hey there, Bruce!");
he.DemoEvent("How are you today?");
he.DemoEvent("I'm pretty good.");
he.DemoEvent("Thanks for asking!");
}
private static void SampleEventHandler(object src, MyEventArgs mea)
{
Console.WriteLine(mea.Message);
}
}
//---------------------------------------------------------
/*
This example produces the following results:
Hey there, Bruce!
How are you today?
I'm pretty good.
Thanks for asking!
*/
相关文章推荐
- ASP.NET深入浅出系列4- 也谈委托和事件
- ASP.NET中定制自己的委托和事件参数类
- ASP.NET深入浅出系列2-页面生命周期详解
- Asp.net用户控件和委托事件
- ASP.NET深入浅出系列3- Page类
- 用简单的代码测一测你是否真的了解:Asp.Net中的事件和委托的实现
- 《ASP.NET AJAX深入浅出系列课程》下载地址
- ★★★【庖丁解牛:纵向切入Asp.net 3.5控件和组件开发技术系列—(5)事件和数据回发机制】★★★
- ASP.NET AJAX深入浅出系列----UpdatePanel的使用笔记(上)
- MSDN Webcast“深入浅出ASP.NET AJAX系列” [转载]
- 3.1依赖注入「深入浅出ASP.NET Core系列」
- 3.1依赖注入「深入浅出ASP.NET Core系列」
- 目录导航「深入浅出ASP.NET Core系列」
- 有效利用ASP.NET用户控件的事件委托
- [ASP.NET开发系列]在用户控件中添加事件 -- 摘自 tigerwen01
- 也谈ASP.NET页面事件
- 自定义事件(自定义控件)在asp.net中委托和事件使用的基本例子
- ASP.NET温故而知新学习系列之ASP.NET多线程编程—.NET下的多线程编程Thread中委托的使用(六)
- ASP.NET深入浅出系列 - 页面生命周期详解
- ASP.NET AJAX深入浅出系列课程