今天讲了一个很牛X的东西,委托,这东西可以说是上天入地无所不能。下面要
 
说的是,定义委托的位置。
 
下面是教科书上的委托定义方法
namespace Weituo            
{        
        //像方法一样定义委托
        delegate void DL(int i);
        class Program
        {
                //定义和委托参数相同的方法
                public void ff(int i)
                {
                 Console.WriteLine("ff");
                 Console.WriteLine(i);
                }
                static void Main(string[] args)
                {
                        Program p = new Program();
                        DL dl = new DL(p.ff);//调用委托
                        dl(2);//直接赋值
                        
                        DY(dl);//方法调用赋值

                        Myclass mc = new Myclass();//类调用赋值
                        mc.yy(dl);
                }
                //在方法中调用委托
                static void DY(DL dl)
                {
                        dl(100);
                }

        }
        //在另一个类中调用委托
        class Myclass
        {
                public void yy(DL kk)
                {
                        kk(200);
                }
        }
}
输出结果为:
ff
2
ff
100
ff
200
上例中,class Myclass 委托 在同一个命名空间下,同时委托不在别的类中。
 
然后我就考虑,如果委托命名在别的类中,如上例,将委托定义在class Program
 
class Myclass 是否还能访问到委托。于是有了以下这段代码:
namespace Weituo            
{        
        class Program
        {
                //像方法一样定义委托
                public delegate void DL(int i);
                //定义和委托参数相同的方法
                public void ff(int i)
                {
                 Console.WriteLine("ff");
                 Console.WriteLine(i);
                }
                static void Main(string[] args)
                {
                        Program p = new Program();
                        DL dl = new DL(p.ff);//调用委托
                        dl(2);//直接赋值
                        
                        DY(dl);//方法调用赋值

                        Myclass mc = new Myclass();//类调用赋值
                        mc.yy(dl);
                }
                //在方法中调用委托
                static void DY(DL dl)
                {
                        dl(100);
                }

        }
        //在另一个类中调用委托
        class Myclass
        {
                public void yy(DL kk)
                {
                        kk(200);
                }
        }
}
 
系统报错。找不到My class中的 DL。于是求助于老桂。
 
修改后如下:
 
namespace Weituo            
{        
        class Program
        {
                //像方法一样定义委托
                public delegate void DL(int i);
                //定义和委托参数相同的方法
                public void ff(int i)
                {
                 Console.WriteLine("ff");
                 Console.WriteLine(i);
                }
                static void Main(string[] args)
                {
                        Program p = new Program();
                        DL dl = new DL(p.ff);//调用委托
                        dl(2);//直接赋值
                        
                        DY(dl);//方法调用赋值

                        Myclass mc = new Myclass();//类调用赋值
                        mc.yy(dl);
                }
                //在方法中调用委托
                static void DY(DL dl)
                {
                        dl(100);
                }

        }
        //在另一个类中调用委托
        class Myclass
        {
                public void yy(Program.DL kk)
                {
                        kk(200);
                }
        }
}
原来委托也是一种类型,所以应该像上例中如此使用。
 
拜谢老桂,并如果博客中有错误请指正。