您的位置:程序门 -> .net技术 -> c#



c#中委托和事件有什么关系和区别`??


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


c#中委托和事件有什么关系和区别`??
发表于:2007-04-12 18:36:48 楼主
我是一名在校大学生``
在学习c#.net`~
但是关于这个的``
我没听明白,望各位大哥大姐教一下?
发表于:2007-04-12 18:38:121楼 得分:0
using   system;

namespace   exam
{
  public   delegate   void   delegateendexam(datetime   t,student   s);//委托

  public   class   student
  {
    private   string   _name;
    public   event   delegateendexam   finishexam;

    public   student(string   name)
    {
      _name=name;
    }

    public   void   begintest(datetime   bt)
    {
      console.writeline( "{0}\t{1}开始答题 ",_name,bt);
    }

    public   void   handinpaper()
    {
      console.writeline( "{0}\t答题完成 ",this.tostring());
      finishexam(datetime.now,this);
    }

    public   override   string   tostring()
    {
      return   _name;
    }

  }

  public   delegate   void   delegatebeginexam(datetime   t);

  class   teacher
  {
    public   event   delegatebeginexam   beginexam;

    public   teacher()
    {}

    public   void   sendbeginexam()
    {
      beginexam(datetime.now);
    }

    public   void   acceptpaper(datetime   t,student   s)
    {
      console.writeline( "收{0}\t的试卷 ",s.tostring());
    }

  }

  public   class   exam
  {
    public   exam()
    {}

    public   static   void   main()
    {
      teacher   teacher=new   teacher();
      student[]   students=new   student[5];
      int   index=0;
      students[index++]=new   student( "jack ");
      students[index++]=new   student( "tom ");
      students[index++]=new   student( "jim ");
      students[index++]=new   student( "baker ");
      students[index++]=new   student( "lucy ");

      foreach(student   tr   in   students)
      {
        teacher.beginexam+=new   delegatebeginexam(tr.begintest);
        tr.finishexam+=new   delegateendexam(teacher.acceptpaper);
      }

      teacher.sendbeginexam();
      foreach(student   stu   in   students)
      {
        stu.handinpaper();
      }
    }
  }
}
发表于:2007-04-12 18:38:442楼 得分:0
http://kv4000.cnblogs.com/articles/122128.aspx
发表于:2007-04-12 18:39:443楼 得分:0
看msdn中讲述“委托”的章节。权威准确。
发表于:2007-04-12 19:36:444楼 得分:0
我这两天也在研究这东西,,给你几个关于委托的例子,都是调试通过的,

1、
=======================================
visual   basic     复制代码  
imports   system

public   class   samplesdelegate

      '   declares   a   delegate   for   a   method   that   takes   in   an   int   and   returns   a   string.
      delegate   function   mymethoddelegate(myint   as   integer)   as   [string]

      '   defines   some   methods   to   which   the   delegate   can   point.
      public   class   mysampleclass

            '   defines   an   instance   method.
            public   function   mystringmethod(myint   as   integer)   as   [string]
                  if   myint   >   0   then
                        return   "positive "
                  end   if
                  if   myint   <   0   then
                        return   "negative "
                  end   if
                  return   "zero "
            end   function   'mystringmethod

            '   defines   a   static   method.
            public   shared   function   mysignmethod(myint   as   integer)   as   [string]
                  if   myint   >   0   then
                        return   "+ "
                  end   if
                  if   myint   <   0   then
                        return   "- "
                  end   if
                  return   " "
            end   function   'mysignmethod
      end   class   'mysampleclass

      public   shared   sub   main()

            '   creates   one   delegate   for   each   method.
            dim   mysc   as   new   mysampleclass()
            dim   myd1   as   new   mymethoddelegate(addressof   mysc.mystringmethod)
            dim   myd2   as   new   mymethoddelegate(addressof   mysampleclass.mysignmethod)

            '   invokes   the   delegates.
            console.writeline( "{0}   is   {1};   use   the   sign   " "{2} " ". ",   5,   myd1(5),   myd2(5))
            console.writeline( "{0}   is   {1};   use   the   sign   " "{2} " ". ",   -   3,   myd1(-   3),   myd2(-   3))
            console.writeline( "{0}   is   {1};   use   the   sign   " "{2} " ". ",   0,   myd1(0),   myd2(0))

      end   sub   'main

end   class   'samplesdelegate  


'this   code   produces   the   following   output:
'  
'5   is   positive;   use   the   sign   "+ ".
'-3   is   negative;   use   the   sign   "- ".
'0   is   zero;   use   the   sign   " ".


 
c#     复制代码  
using   system;
public   class   samplesdelegate     {

      //   declares   a   delegate   for   a   method   that   takes   in   an   int   and   returns   a   string.
      public   delegate   string   mymethoddelegate(   int   myint   );

      //   defines   some   methods   to   which   the   delegate   can   point.
      public   class   mysampleclass     {

            //   defines   an   instance   method.
            public   string   mystringmethod   (   int   myint   )     {
                  if   (   myint   >   0   )
                        return(   "positive "   );
                  if   (   myint   <   0   )
                        return(   "negative "   );
                  return   (   "zero "   );
            }

            //   defines   a   static   method.
            public   static   string   mysignmethod   (   int   myint   )     {
                  if   (   myint   >   0   )
                        return(   "+ "   );
                  if   (   myint   <   0   )
                        return(   "- "   );
                  return   (   " "   );
            }
      }

      public   static   void   main()     {

            //   creates   one   delegate   for   each   method.
            mysampleclass   mysc   =   new   mysampleclass();
            mymethoddelegate   myd1   =   new   mymethoddelegate(   mysc.mystringmethod   );
            mymethoddelegate   myd2   =   new   mymethoddelegate(   mysampleclass.mysignmethod   );

            //   invokes   the   delegates.
            console.writeline(   "{0}   is   {1};   use   the   sign   \ "{2}\ ". ",   5,   myd1(   5   ),   myd2(   5   )   );
            console.writeline(   "{0}   is   {1};   use   the   sign   \ "{2}\ ". ",   -3,   myd1(   -3   ),   myd2(   -3   )   );
            console.writeline(   "{0}   is   {1};   use   the   sign   \ "{2}\ ". ",   0,   myd1(   0   ),   myd2(   0   )   );
      }

}


/*
this   code   produces   the   following   output:
 
5   is   positive;   use   the   sign   "+ ".
-3   is   negative;   use   the   sign   "- ".
0   is   zero;   use   the   sign   " ".
*/  

 
=========================================
发表于:2007-04-12 19:37:555楼 得分:0
2、
=====================================
using   system;


public   class   delcls
        {
        public   delegate   void   deldef(params   string[]   strparams);

        public   static   void   calldel(deldef   dd)
                {
                if(dd   !=   null)       //请务必在此处进行判断,这是个好习惯
                        {
                        dd( "hello ", "world ");
                        }
                }
        }


public   class   delins
        {
        //声明为private(私有)成员并不影响在类型内部使用委托
        private   static   void   clscallstr(params   string[]   strparams)           //类型方法
                {
                //将字符串数组并顺序输出
                foreach(string   str   in   strparams)
                        {
                        console.write( "{0}   ",str);
                        }
                console.writeline(   );
                }


        public   void   inscallstr(params   string[]   strparams)                             //实例方法
                {
                //将字符串数组并反序输出
                for(int   i   =   strparams.length   -   1;   i   > =   0;   i--)
                        {
                        console.write( "{0}   ",strparams[i]);
                        }

                console.writeline(   );
                }


        public   static   void   main(   )
                {
                delins   di   =   new   delins(   );
                delcls.deldef   dd   =   null;
                console.writeline( "combine   two   delegate: ");
                dd   +=   new   delcls.deldef(delins.clscallstr);
                dd   +=   new   delcls.deldef(di.inscallstr);

               
                dd.getinvocationlist(   );//返回委托链中的内容。

                delcls.calldel(dd);

                console.writeline( "remove   the   first   delegate: ");
                dd   -=   new   delcls.deldef(delins.clscallstr);
                delcls.calldel(dd);

                }

        }
发表于:2007-04-12 19:40:386楼 得分:0
个人理解
通常意义上的委托是一种编程的方法和思想。而事件,是实现这种方法的一个典型代表
发表于:2007-04-12 19:59:517楼 得分:0
delegate是event他爹兼他妈~
发表于:2007-04-12 22:08:198楼 得分:0
谢谢各位`~
不知能否将注释写进去`~


快速检索

最新资讯
热门点击