F
Frankie
The following questions are with respect to this delegate declaration:
delegate void MyDelegate(string s);
Question 1:
Why does the following NOT work? Why can't I just create a new instance of
MyDelegate that references zero methods?
MyDelegate d;
d = new MyDelegate();
Question 2:
Given that the following code successfully registers two methods with the
MyDelegate instance...
MyDelegate d = new MyDelegate(MyMethod)
d += new MyDelegate(MyOtherMethod);
.... why do we have to register the additional method (MyOtherMethod) via a
NEW MyDelegate instance? The above code (d += new MyDelegate...) makes it so
we are adding an instance of a delegate to an instance of a delegate. When
our objective is "simply to register an additional method with a delegate
instance" why can we not simply do something like:
d.Add(MyOtherMethod); // I know, there is no Add method - but why not?
or
d += MyOtherMethod;
I thought that delegates maintained an internal linked list of "methods to
call" - I would therefore expect a simple .Add() method that lets us add
method references to a delegate instance.
Question 3:
When we want to remove a method reference from a delegate, we still must use
the 'new' keyword, like this:
d -= new MyDelegate(MyMethod);
Why do we have to use the 'new' keyword when UNregistering a method
reference?
Thanks!
delegate void MyDelegate(string s);
Question 1:
Why does the following NOT work? Why can't I just create a new instance of
MyDelegate that references zero methods?
MyDelegate d;
d = new MyDelegate();
Question 2:
Given that the following code successfully registers two methods with the
MyDelegate instance...
MyDelegate d = new MyDelegate(MyMethod)
d += new MyDelegate(MyOtherMethod);
.... why do we have to register the additional method (MyOtherMethod) via a
NEW MyDelegate instance? The above code (d += new MyDelegate...) makes it so
we are adding an instance of a delegate to an instance of a delegate. When
our objective is "simply to register an additional method with a delegate
instance" why can we not simply do something like:
d.Add(MyOtherMethod); // I know, there is no Add method - but why not?
or
d += MyOtherMethod;
I thought that delegates maintained an internal linked list of "methods to
call" - I would therefore expect a simple .Add() method that lets us add
method references to a delegate instance.
Question 3:
When we want to remove a method reference from a delegate, we still must use
the 'new' keyword, like this:
d -= new MyDelegate(MyMethod);
Why do we have to use the 'new' keyword when UNregistering a method
reference?
Thanks!