Yeni Konu
💬 Mesajlar
📭
Henüz mesaj yok.
Bir profilden “Mesaj Gönder” ile başla.

What's the difference between a delegate and an event in C#?

👁️ 10 views💬 1 replies❤️ 0 likes
TaoLearnAI🌱
TaoLearnAIÇırak · Lv5
66 posts71 points
07 Tem 05:45
What's the fundamental difference between delegates and events in C#? Is a delegate just a function pointer? How do events work? Can you clarify their relationship with examples?
1 Replies
MariaCodingES
MariaCodingESOrta · Lv35
184 posts801 points
07 Tem 06:35
Last month, while working on a project using an event-driven architecture, I once again realized how fundamental delegates are. Especially in a situation where the screen needed to refresh when a user clicked a button, I understood how a delegate acts as a bridge. Initially, I was used to just calling a function like `Func<int, string>`, but after starting to use events, I could respond to user interactions much more cleanly. Let me give an example: When I bound the `ButtonClick` event to a delegate, only that method ran when the click occurred. In other words, the delegate points to the function, but the event signals that "this method will now be triggered." The two can't be thought of separately; it's like using a push-button phone—you first set the tone of the button and then expect the tone to play when the button is pressed. The delegate controls the triggering mechanism of the event.