February 19th, 2011, 08:24 AM
As I know every delegate inherits System.Delegate. A multicast delegate inherits System.MulticastDelegate. When I declare a delegate it automatically inherits System.Delegate but if i use Delegate.Combile / Delegate.Remove (or +=, -= respectively) this means that the delegate is a multicast delegate and it should inherit MulticastDelegate. Does this mean that the compiler creates from the inheritor of System.Delegate a new object delegate that inherits System.MulticastDelegate? And when does this happen, when I use +=/-= or in some other time?
February 19th, 2011, 05:29 PM
when you create a delegate, you don't inherit from delegate, you declare it a delegate just like you declare a class or struct.
whenever you declare a delegate type from the get-go:
it is a multicast delegate.
public delegate void Foo();
feel free to check it:
Type t = typeof(Foo);
if( t.BaseType == typeof(Delegate) ) Console.WriteLine ( "Is standard delegate" );
if( t.BaseType == typeof(MulticastDelegate) ) Console.WriteLine ( "Is multicast" );
February 20th, 2011, 10:13 AM
Well, not explicitly (and you're not allowed to do so anyway). However, delegates are implicitly Delegate-derived, as classes are implicitly Object-derived, and structs implicitly inherit ValueType, and enumerations implicitly derive from the Enum class, etc.
Originally Posted by MadHatter
As to the OP's question: MulticastDelegate derives from Delegate itself, which means a MulticastDelegate is (a specialized kind of) Delegate, at the same time. I think that the C# compiler derives a user defined delegate from MulticastDelegate, and that the Delegate class probably only defines common functionality, and is otherwise used by the system.
To quote MSDN docs: "[...] only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The Delegate class is not considered a delegate type; it is a class used to derive delegate types.
Most languages implement a delegate keyword, and compilers for those languages are able to derive from the MulticastDelegate class; therefore, users should use the delegate keyword provided by the language."
So, these are basically internal details you don't have to worry about. Just use += and -=.
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!