Delegates and events are powerful constructs in C# that allow for flexible and extensible code. While they serve different purposes, they can sometimes be used to achieve similar ends, leading to confusion about when to use one over the other.
What is the difference between delegates and events?
Loading

Mariusz PostolPosted Oct 7, 2024, 9:31 AM
According to the Copilot research; a "delegate" is someone authorized to represent others. To paraphrase this definition, in terms of a programming language, the delegate is a construct representing another language construct. Consequently, using object-oriented programming terminology, a delegate is an object wrapping a reference to a method (an action that happens). The method is invoked while the delegate is triggered. Simplifying, delegate represents an activity.
Continuing the Copilot research; “event” refers to an occurrence or happening that involves one or more entities and can be described by a verb or action. To paraphrase this definition, in terms of a programming language, the event is an action (block of statements) invoked and executed using a computer machine. Consequently, using object-oriented programming terminology, an event may be recognized as an object wrapping a reference to a method (an action that happens). The method is invoked while the event is triggered. Simplifying, an event represents activity.
Hence, in the context of program engineering, despite the everyday meanings of both terms being completely different, they are implemented similarly. However, before implementation, we must learn more details concerning both terms. The most important thing is the difference between them, which helps address the problem of why we need both terms.
I want to close this thread with information that I have just answered on this topic in the video just publisher at c-sharpcorner
The video is available at
Mariusz PostolPosted Sep 25, 2024, 1:56 PM
Let me inform you that the discussion on the event keyword has been finished and the MS documentation modifications have been merged into the main branch. Check out the pr if you need details.
In my opinion, it doesn't answer all doubts hence the work will be continued.
Mariusz PostolPosted Sep 9, 2024, 6:59 PM
@Sam Hobbs
Many thanks for your valuable contribution. The discussion is underway at my pr at. Follow it if you like. Hopefully, you have an account on GitHub.
M
Sam HobbsPosted Sep 9, 2024, 4:53 PM
The list of keywords you provide the link for that is:
Does not help. It does not say what the event keyword is.
I think the fundamental problem is that the event keyword is used in C# in a very confusing manner. A fundamental design criteria for the language is that it be readable yet I think this is an example of a violation of that. I agree that the documentation should be improved.
Mariusz PostolPosted Sep 9, 2024, 1:45 PM
@Sam Hobbs,
Again thanks for the help, you are right - nothing is perfect. The problem is how to fix it. I have a few friends in Microsoft but it doesn't work, I tried to contribute to the documentation on GitHub but it also doesn't work.
BTW the keywords are defined at
As I said, "The meaning of the event depends on the context".
Anyway, I have just proposed the documentation change proposal at
I have pr at but of course, the merge is blocked because of review. We must wait. I suppose there are many places in the documentation that must be reviewed and fixed. Any proposals are welcome.
Sam HobbsPosted Sep 9, 2024, 4:30 AM
Okay you have found where the event keyword is used. When I search for documentation ti says keyword. I wish people would not call verything a keyword. When there is a specific name for what something is then the documentation should say what the keyword is. I can't find an explanation of what the event keyword is. The following is the official specifications that developers of C# compilers would use to write a compiler.
event keyword - C# reference | Microsoft Learn
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/event
Even that does not specify what the event keyword is. Very frustrating.
The following is the programming documentation of event.
event keyword - C# reference | Microsoft Learn
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/event
Note that it says that the
eventkeyword declares an event. Further down is something interesting. It says:And that is where the += and -= becomes relevant. Microsoft very often uses confusing terminology. My guess is that the event keyword makes a delegate a multicast delegate. Whereas in other contexts, such as for other operating systems including the Windows API, an event is very different. Microsoft's use of the term event in the context of .Net seems to be inconsistent with the typical use of the term.
Mariusz PostolPosted Sep 7, 2024, 8:46 AM
@Sam Hobbs. Many thanks for your contribution. There are a few interesting points that need explanation and overcoming some confusion.
1. "MyDelegate is a definition. There must be one and only one definition of something. Storage is not allocated for definitions." - You are perfectly OK. MyDelegate is an identifier - the name of a delegate type. Of course, type doesn't need allocation of memory and finally assigning any value to the type. For me, the whole line is a declaration defining a type. Do you agree?
2. The following declaration defines the "MyEvent" variable, which is a set of pointers to methods with the signature compliant with the signature defined by the MyDelegate type. For me, the declaration is a programming language construct (has syntax and semantics) and a definition is part of the declaration providing implementation details (in this example type of the variable and indication that it is an event using the "event" keyword).
Declaring a variable causes allocation of memory because we can assign values (it is a set) to this variable. Formally we are adding new values using the "+=" operator. From this example we can derive that "event" in language terminology is a variable of delegate type prefixed by the "event" keyword. Do you agree?
Very interesting is the reference to the "There are however
ManualResetEventandAutoResetEventclasses" where we have the event word as part of the identifiers. For me, the use of the event term in these identifiers refers to a condition coupled with concurrent programming and thread synchronization.Concluding
The meaning of the event depends on the context:
1. It is a condition if we are talking about the implementation of an algorithm (knowledge of how to solve a problem, for example, concurrent activities synchronization).
2. Keyword changing the regular variable into an event variable. Referring to the language semantics the variable must be of a delegate type (it is a set of pointers to methods compliant with a predefined signature). The level of abstraction means that we don't know the methods, we only know that all are compliant with the predefined signature.
3. Consequently, signaling an event means that we are calling methods pointed by the set held by the event variable. The event variable may be used in the methods invocation statement and expressions like a regular method.
4. The "event" keyword changes the visibility of selected operations we can execute against the event variable despite the access modifier of the variable. For example, we cannot invoke pointed methods outside of the class where the event variable is declared even if the variable is public. All these operations are only private. In other words, the "event" keyword is an access modifier replacement for selected operations of the delegate type.
5. We must recognize delegate type and delegate variable. Hence, the delegate term without postfix type/variable doesn't make any sense in the context of the language definition.
Any comments and questions are welcome. I am not sure how this is compliant with the language documentation. Maybe an independent article or video lesson could help.
Rdgs,
Sam HobbsPosted Sep 6, 2024, 8:48 PM
In the following example that you provided:
MyDelegateis a definition. There must be one and only one definition of something. Storage is not allocated for definitions.delegateVariableis a declaration. Storage is allocated for declarations. For delegates, storage is allocated for a pointer to a method with a matching signature.Sam HobbsPosted Sep 6, 2024, 8:29 PM
I think there is no such thing as an
Eventclass or type in .Net. There are howeverManualResetEventandAutoResetEventclasses.The term event is a term used with all multiprocessing operating systems. Events are synchronization objects. IBM Mainframe operating systems had events half a century ago. Windows had events in the original 16-bit versions of Windows, as in:
Using Event Objects (Synchronization) - Win32 apps | Microsoft Learn
https://learn.microsoft.com/en-us/windows/win32/sync/using-event-objects
As explained in other answers, delegates are a way to define a type of method. A delegate has no implementation; nothing to execute. A method with a matching signature can be assigned to a delegate type.
In the context of delegates the term event typically has a different meaning. In .Net an event is something that has happened, such as data has become available. A delegate type is used for the program that the event has occured in to allow (by calling a specified method) another program to do something.
I would not say that an event is a type of delegate. Microsoft very often uses names and terms that confuse us and I can understand that it is easy to think that an event is a type of delegate.
Mariusz PostolPosted Sep 6, 2024, 7:22 AM
@Naimish Makwana, thanks for your contribution.
"A delegate is a type that represents references" - is delegate also a variable of delegate type?
Is it the correct snippet?
"An event is a special kind of delegate" - is it a special kind of type? It looks like the event is also a variable. Is the "event" also a keyword? For example:
Is it the correct snippet?
" They are more flexible " - How to prove this statement? Do you know any measure we can apply to compare?
"..ensuring that subscribers are notified when an event occurs." what is a notification in terms of the programming language? What is the event in this sentence (type, variable, method, method invocation, or condition)?
Naimish MakwanaPosted Sep 6, 2024, 5:06 AM
Delegates and events in C# are indeed powerful tools, and understanding their differences can help you use them more effectively.
Delegates
Events
Key Differences
In summary, use delegates when you need to pass methods around or implement callbacks, and use events when you need to notify multiple subscribers about something that has happened.
Thanks
Gowtham CpPosted Sep 6, 2024, 5:00 AM
Hello Mariusz Postol ,
Delegates are like a direct way to store and call methods. Think of them as pointers to method - once you assign a method to a delegate, you can call it whenever you want. It’s like giving someone a remote control to trigger certain actions.
Example: You assign a method to a delegate, and then anyone with access to that delegate can run that method. Simple, but a bit risky because anyone can call it anytime.
Events, on the other hand, are built on top of delegates but with more rules. They allow you to let other parts of your program know when something happens (like a button click or a file download completing). However, the event can only be triggered by the class that owns it, making it safer.
Example: Think of events like a newsletter subscription - you can subscribe to get notified, but only the publisher can send out the actual notification. Subscribers (listeners) can only react when something happens, but they can’t trigger the event themselves.
So, use delegates when you want flexibility in calling methods, and events when you want a safer, notification-based system where others can only listen but not trigger actions.
Hope it helps!
Prasad RaveendranPosted Sep 6, 2024, 12:14 AM
-
- Delegate: Any code that knows about the delegate can call it.
- Event: Only the class or structure that owns the event can trigger it, keeping it more secure.
-
- Delegate: Used to pass methods as arguments or for setting up custom actions. It gives flexibility in how the code can be extended or customized.
- Event: Used to let different objects know when something important happens, making the communication between them more organized and separated.
-
- Delegate: Anyone who knows the delegate can access and call it directly.
- Event: Other objects can only subscribe (listen) or unsubscribe from the event, but they can't trigger it themselves.
When to Use Which:Invocation:
Purpose:
Access Control: