Original Post
📌 Pinned
🔒 Locked
C# Workshop - Week 9 (Ch. 24 - 26)
Advertisement
I don't get events and delegates. The text doesn't really give me a good explanation. Can anyone provide an example? I'm just not seeing how to use it.
Quote:
Original post by Alpha_ProgDes
I don't get events and delegates. The text doesn't really give me a good explanation. Can anyone provide an example? I'm just not seeing how to use it.
Delegates are unfortunately confusing since the term is used for multiple things.
The first term is for the keyword delegate:
public delegate void FooType(int Param);This defines a delegate type. (note that it's often referred to as "delegate type" in common speech). If you've used C++, this is a typedef for void(int).
public FooType FooDelegate;This creates an instance of FooType. This is also referred to as a delegate. It is the equivalent of a function object or function pointer in other languages. In this particular code though, FooDelegate is public, so anyone can assign to it, call it, append extra delegates to it (since delegates can contain more than one delegate, calling each in turn).
That kinda sucks, and makes debugging difficult so there's the event keyword.
public event FooType FooEvent;The event keyword is a modifier to public. It makes operator += and -= public, but everything else private. (yes private, subclasses cannot invoke the delegate)'
Hope that helps.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement