找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 10596|回复: 2

关于delegate的例子

[复制链接]
发表于 2009-11-29 02:12:07 | 显示全部楼层 |阅读模式 IP:江苏扬州
示例一:委托被同时映射到静态方法和实例方法,并分别返回特定的信息
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class delegate1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SampleClass sc = new SampleClass();
        // Map the delegate to the instance method:
        Del d = sc.InstanceMethod;
        d();
        // Map to the static method:
        d = SampleClass.StaticMethod;
        d();
    }
}
delegate void Del();
class SampleClass
{
    public void InstanceMethod()
    {
        HttpContext.Current.Response.Write("A message from the instance method.");
    }
    static public void StaticMethod()
    {
        HttpContext.Current.Response.Write("A message from the static method.");
    }
}
示例二
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
delegate void Del(int i, double j);
public partial class _Default :Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            Del d = MultiplyNumbers;
            Response.Write("Invoking the delegate using 'MultiplyNumbers':");
            for (int i = 1; i <= 5; i++)
            {
                d(i, 2);
            }
        }
    }
    protected void MultiplyNumbers(int m, double n)
    {
        HttpContext.Current.Response.Write(m * n + " ");
    }
   
}
发表于 2010-3-6 12:05:06 | 显示全部楼层 IP:印度尼西亚
楼主,你写得实在是太好了。我惟一能做的,就只有把这个帖子顶上去这件事了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 03:26 , Processed in 0.299316 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表