1

As the MulticastDelegate is ruling delegate class of .NET I would like to ask, are there any use cases for using Delegate class?

I mean if I had a variable of type Delegate and another one of type MulticastDelegate, different method overriddes (e.g GetInvocationList) will be called but if every single-programmer defined delegate inherits from MulticastDelegate what could be a point for using Delegate class?

And if as I assume there is none, shouldn't it be marked with ObsoleteAttribute with comment to use MulticastDelegate instead?

Or shouldn't compiler silently change "Delegate variable1;" to "MulticastDelegate variable1;"?

user1121956
  • 1,843
  • 2
  • 20
  • 34
  • You should virtually never be using the `Delegate` or `MulticastDelegate` types directly. Any specific delegates defined will actually be of both types (a `MulticastDelegate` is a sub-type of `Delegate`) but you should only ever refer to delegates by their specific type, with a defined signature. – Servy Mar 19 '13 at 14:00
  • but what about generalized list of delegates for example I would use List there as I don't know method signatures – user1121956 Mar 19 '13 at 14:02
  • @user1121956: What benefit does that give you over `List`? – Jon Skeet Mar 19 '13 at 14:03
  • @user1121956 If you don't know the method signatures what do you plan to do with the delegates? You can't invoke them as you don't know what their parameters are, and you won't be able to use the return value because you won't know if it has one, or what it might be. – Servy Mar 19 '13 at 14:03
  • Dupe: [is-there-a-delegate-which-isnt-a-multicastdelegate-in-c](http://stackoverflow.com/questions/4711118/is-there-a-delegate-which-isnt-a-multicastdelegate-in-c) – nawfal Jul 07 '14 at 17:43

1 Answers1

4

It's not obsolete, and I see no reason to mark it as such.

If the only members you need are provided by Delegate, why would you use MulticastDelegate?

In my experience Delegate comes up much more often in the .NET API as "any delegate" than MulticastDelegate does. For example:

It's definitely only due to historical accident that the two classes both exist, but that's no reason to deprecate uses of just Delegate.

Additionally, you write:

I mean if I had a variable of type Delegate and another one of type MulticastDelegate, different method overloads (e.g GetInvocationList) will be called

No, GetInvocationList isn't overloaded. It's overridden in MulticastDelegate, but not overloaded. Oh, and note the return type of that method - it's Delegate[], not MulticastDelegate[].

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • sure it's overridden not overloaded, I mistyped this in hurryness, stupid error XD – user1121956 Mar 19 '13 at 14:36
  • and that's exactly my point, if I call GetInvocationList of MulticastDelegate type I will get different result than Delegate type and it will be error prone for program which would like to do `for (int i ...) List[i].DynamicInvoke(arg_list[i])` – user1121956 Mar 19 '13 at 14:39
  • @user1121956: No, you *won't* get a different result, precisely *because* it's overridden. The `MulticastDelegate` implementation will be called anyway. `foo.GetInvocationList()` will execute the exact same code whether the compile-time type of `foo` is `MulticastDelegate` or `Delegate`. If you believe otherwise, you need to revise your understanding of how virtual methods work. – Jon Skeet Mar 19 '13 at 15:12
  • oh lol, you're perfectly right, in the amok of thinking I forgot that all delegates are Multicast anyway, thank you :) – user1121956 Mar 19 '13 at 15:33