6

Is

public event Action delt = () => { Console.WriteLine("Information"); };

an overloaded version of

Action<int, int> delg = (a, b) => { Console.WriteLine( a + b); }; ?

I mean Action<> delegate is an overloaded version of "event Action"?

user160677
  • 4,233
  • 12
  • 42
  • 55

3 Answers3

7

It's not called an overload.

Basically, there is a set of types, declared like this:

namespace System {
    delegate void Action();
    delegate void Action<T>(T a);
    delegate void Action<T1, T2>(T1 a1, T2 a2);
    ...
}

Each of them is a different type, independent of all other. The compiler knows which type you mean when you try to reference it by the presence or absence of <> after the type name, and the number of type parameters within <>.

event is a different thing altogether, and doesn't play any part in this. If you're confused by the difference between event and delegate, see these two questions: 1 2

Community
  • 1
  • 1
Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
2

The event isn't "Action", it is called 'delt', and it has an EventHandler delegate of type Action. Normally you'd want your events to have an EvenHandler conforming to the standard eventing model (e.g. MyHandler(object sender, InheritsFromEventArgs argument))

Action and Action<> are delegate types and exist as part of the System namespace.

darthtrevino
  • 2,205
  • 1
  • 15
  • 17
1

Strictly speaking they are not equivalent. The first assigns an event to an anonymous function while the second declares an anonymous function. Events just like properties have special meaning in the CLR and follow some design guidelines.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928