Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action<> Vs event Action

Tags:

c#

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

like image 251
user160677 Avatar asked Nov 17 '09 17:11

user160677


2 Answers

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

like image 99
Pavel Minaev Avatar answered Sep 28 '22 10:09

Pavel Minaev


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.

like image 21
darthtrevino Avatar answered Sep 28 '22 09:09

darthtrevino