private delegate void mydelegate(int n);
mydelegate p;
请问这个p是委托的引用还是委托的对象?如果是委托的引用,是不是说在创建委托引用之后,就默认地实例化委托了?

解决方案 »

  1.   

    p是委托
    但是p当前为null
    因为还没实例化
    要实例化,则需要newmydelegate p = new mydelegate(方法);
      

  2.   

    p是委托
    但是p当前为null
    因为还没实例化
    要实例化,则需要newmydelegate p = new mydelegate(要委托的方法);
      

  3.   


    既然没有实例化,那应该p只是委托的引用呀,它本身并不是委托对象;实际上:
    mydelegate p;
    p = func;
    p += func;
    ……
    这样就可以。
    可这也没有对委托进行显式地实例化呀?
      

  4.   

    是这个委托的对象,用编译器可以获得这个委托类的所有成员,包括给这个对象赋予实现的函数。我做了实验,以下代码可以使用这个委托(代理)
     private delegate void mydelegate(int n);
            public static void show(int n)
            {
                Console.WriteLine(n);
            }
            static void Main(string[] args)
            {
                mydelegate p = new mydelegate(show);
                p(50);        } 
      

  5.   


    我也这样认为,可是 mydelegate p = func 这样就可以,
    这个p的实例化在哪里呢?是不是在初始化或者第一次调用“=”
    操作符时默认初始化了????
      

  6.   


    这并不能说明p是委托对象啊,倒是有可能是reference
      

  7.   


    所以这种情况下是什么时候实例化委托的呢:
    mydelegate p;
    p = 要委托的方法;有没可能以上赋值的时候,就先实例化委托了(new mydelegate())?
      

  8.   

    我想大概是这样的吧:
    在C#中实际上所有数据类型都是对象,即便是int也是对象。
    因此,mydelegate p 创建了一个p的变量(其实也是对象),
    只是delegate内部重载了“=”操作符,通过调用“=”操作符
    来指向方法。
      

  9.   

    委托引用 这个词有点陌生,这个p就是委托实例或对象了和class A{}A a;是一样的
      

  10.   


    说的是CPP还是Java?
    在CPP中a是对象,在Java中a是引用.
      

  11.   


    我就是学C++和Java的,在CPP中这样写的话是在栈中创建实例
      

  12.   


    我觉得这个问题有可能是这样:在C#中,定义委托实际上是定义一个类
    因此,尽管对函数来说,它是一个引用,但它本身是一个对象。

    看来楼主武功学太杂,已经开始互搏了,C#中象这样的委托定义delegate void X(int i,string s);这里的X和定义和一个类的级别是相同的就象定义一个enum struct差不多,
    委托实际上的基类是System.Delegate只是这个类不能被用记继承而已,所以委托都继承于这个基类的
    委托所代表就是一个函数引用 你说得没错,每个委托的实例都是一个函数的引用 而X x1,x2;x1=this.Method1;
    x2=this.Method2;这个自然为使用类申明一个对象级别是相同或差不多的。
      

  13.   


    internal delegate void Feedback(Int32 value);
    When it sees this line, the compiler actually defines a complete class that looks something like this:internal class Feedback : System.MulticastDelegate

    // Constructor 
    public Feedback(Object object, IntPtr method); 
    // Method with same prototype as specified by the source code 
    public virtual void Invoke(Int32 value); 
    // Methods allowing the callback to be called asynchronously 
    public virtual IAsyncResult BeginInvoke(Int32 value, 
    AsyncCallback callback, Object object); 
    public virtual void EndInvoke(IAsyncResult result); 
    }
      

  14.   


    不是的。c#不是javascript。其实这是编译器做的事,当你写p = func;的时候编译出来的代码实际上是p = new mydelegate(func);.net并不支持什么函数赋值给delegate,纯粹是编译器帮你简化语法。
      

  15.   

    Syntactical Shortcut #1: No Need to Construct a Delegate ObjectAs demonstrated already, C#  allows you to specify the name of a callback method without having to construct a delegate object wrapper. Here is another example:
    internal sealed class AClass
    {
        public static void CallbackWithoutNewingADelegateObject()
        {
            ThreadPool.QueueUserWorkItem(SomeAsyncTask, 5);
        }
        private static void SomeAsyncTask(Object o)
        {
            Console.WriteLine(o);
        }
    }
    Here, the ThreadPool class's static QueueUserWorkItem method expects a reference to aWaitCallback delegate object that contains a reference to the SomeAsyncTask method. Since the C# compiler is capable of inferring this on its own, it allows me to omit code that constructs the WaitCallback delegate object, making the code much more readable and understandable. Of course, when the code is compiled, the C# compiler does produce IL that does, in fact, new up the WaitCallback delegate object—we just got a syntactical shortcut.Traceback: CLR via C#
      

  16.   


    我感觉这样子设计反而会让人混淆……,以为没有new mydelegate(),呵呵