user160677
user160677

Reputation: 4303

Action<> Vs event Action

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"?

Upvotes: 6

Views: 8290

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

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.

Upvotes: 1

Pavel Minaev
Pavel Minaev

Reputation: 101665

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

Upvotes: 7

darthtrevino
darthtrevino

Reputation: 2235

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.

Upvotes: 2

Related Questions