Delegates
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Delegates

  1. #1
    Join Date
    Feb 2011
    Posts
    1

    Delegates

    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?

  2. #2
    Join Date
    Mar 2004
    Location
    3311'18.10"N 9645'20.28"W
    Posts
    1,808

    Re: Delegates

    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:

    Code:
    public delegate void Foo();
    it is a multicast delegate.

    feel free to check it:

    Code:
    Type t = typeof(Foo);
    if( t.BaseType == typeof(Delegate) ) Console.WriteLine ( "Is standard delegate" );
    if( t.BaseType == typeof(MulticastDelegate) ) Console.WriteLine ( "Is multicast" );

  3. #3
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Delegates

    Quote Originally Posted by MadHatter View Post
    when you create a delegate, you don't inherit from delegate, you declare it a delegate just like you declare a class or struct.
    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.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center