Understanding C# Events
I am surprised how often C# developers are confused about delegates and events. They sometimes forget how all C# events are based on delegates and what the delegates offer for C# events. As well, some are confused about how to publish and subscribe to events and how to pass information when raising an event from a publisher to subscriber. Read more to learn about understanding C# events
In this understanding C# events tutorial, I will explain how delegates are the foundation for C# events and show the two primary delegates that Microsoft has given us for creating our own events. I will also show you how to subscribe to your own events and even pass data back to the event handlers.
Understanding C# Events:
What
Delegates Do
Every single event in .NET, whether Microsoft created it or if it was created by someone else, is based on a .NET delegate. Delegates are one of the five types of types included with .NET – class, structure, interface, enumeration, and delegate.
At its foundation, delegates do two things:
- When created, it points to a method (instance or static) in a container (class or structure). For events, it points to an event hander method.
- It defines exactly the kind of methods that it can point to, including the number and types of parameters and also the return type.
Understanding C# Events:
Definition of a
Simple Delegate
PHByZSBjbGFzcz0ibGFuZzpjIyBkZWNvZGU6dHJ1ZSAiPnB1YmxpYyBkZWxlZ2F0ZSBpbnQgZGdQb2ludGVyKGludCBhLCBpbnQgYik7PC9wcmU+
cHVibGljIGRlbGVnYXRlIGludCBkZ1BvaW50ZXIoaW50IGEsIGludCBiKTsKCmNsYXNzIFByb2dyYW0KewogIHN0YXRpYyB2b2lkIE1haW4oKQogIHsKICAgIEFkZGVyIGEgPSBuZXcgQWRkZXIoKTsKICAgIGRnUG9pbnRlciBwQWRkZXIgPSBuZXcgZGdQb2ludGVyKGEuQWRkKTsgICAgCiAgICBpbnQgaUFuc3dlciA9IHBBZGRlcig0LCAzKTsKICAgIENvbnNvbGUuV3JpdGVMaW5lKCJpQW5zd2VyID0gezB9IiwgaUFuc3dlcik7CiAgICAvLyBSZXR1cm5zIOKAnGlBbnN3ZXIgPSA34oCdCiAgfQp9CgpwdWJsaWMgY2xhc3MgQWRkZXIKewogIHB1YmxpYyBpbnQgQWRkKGludCB4LCBpbnQgeSkKICB7IHJldHVybiB4ICsgeTsgfQp9
Let’s assume we want to raise an event in the Adder class if the sum of the two numbers in Add() is a multiple of five (5). We can define an event based on the delegate. This event will be used to raise a notification to run event handlers assigned to it.
NOTE: All C# events in .NET are based on delegates.
NOTE: Wherever you want to raise an event, you must also define the event.
NOTE:You must never raise (publish) an event unless at least one object is listening (subscribing) to the event. In other words, the event must not equal null.
NOTE: A Microsoft Best Practice: All events should be defined starting with the word “On”.
cHVibGljIGNsYXNzIEFkZGVyCnsKICBwdWJsaWMgZGVsZWdhdGUgdm9pZCBkZ0V2ZW50UmFpc2VyKCk7CiAgcHVibGljIGV2ZW50IGRnRXZlbnRSYWlzZXIgT25NdWx0aXBsZU9mRml2ZVJlYWNoZWQ7CiAgCiAgcHVibGljIGludCBBZGQoaW50IHgsIGludCB5KQogIHsKICAgIGludCBpU3VtID0geCArIHk7CiAgICBpZiAoKGlTdW0gJSA1ID09IDApICYmIChPbk11bHRpcGxlT2ZGaXZlUmVhY2hlZCAhPSBudWxsKSkKICAgIHsgT25NdWx0aXBsZU9mRml2ZVJlYWNoZWQoKTsgfQogICAgcmV0dXJuIGlTdW07CiAgfQp9
Y2xhc3MgUHJvZ3JhbQp7CiAgc3RhdGljIHZvaWQgTWFpbigpCiAgewogICAgQWRkZXIgYSA9IG5ldyBBZGRlcigpOwogICAgYS5Pbk11bHRpcGxlT2ZGaXZlUmVhY2hlZCArPSBuZXcgQWRkZXIuZGdFdmVudFJhaXNlcihhX011bHRpcGxlT2ZGaXZlUmVhY2hlZCk7CgogICAgaW50IGlBbnN3ZXIgPSBhLkFkZCg0LCAzKTsKICAgIENvbnNvbGUuV3JpdGVMaW5lKCJpQW5zd2VyID0gezB9IiwgaUFuc3dlcik7CiAgICBpQW5zd2VyID0gYS5BZGQoNCwgNik7CiAgICBDb25zb2xlLldyaXRlTGluZSgiaUFuc3dlciA9IHswfSIsIGlBbnN3ZXIpOwogICAgQ29uc29sZS5SZWFkS2V5KCk7CiAgfQoKICBzdGF0aWMgdm9pZCBhX011bHRpcGxlT2ZGaXZlUmVhY2hlZCgpCiAgewogICAgQ29uc29sZS5Xcml0ZUxpbmUoIk11bHRpcGxlIG9mIGZpdmUgcmVhY2hlZCEiKTsKICB9Cn0=
YS5Pbk11bHRpcGxlT2ZGaXZlUmVhY2hlZCArPSBhX011bHRpcGxlT2ZGaXZlUmVhY2hlZDs=
Here are the two built-in delegates:
cHVibGljIGRlbGVnYXRlIHZvaWQgRXZlbnRIYW5kbGVyKG9iamVjdCBzZW5kZXIsIEV2ZW50QXJncyBlKTsKcHVibGljIGRlbGVnYXRlIHZvaWQgRXZlbnRIYW5kbGVyPFRFdmVudEFyZ3M+KG9iamVjdCBzZW5kZXIsIFRFdmVudEFyZ3MgZSk7
The first delegate is used simply to raise a notification, an event signifying that something happened. The second delegate allows you to return one or more values to the event handler method. It requires you to create an instance of a class that derives from the EventArgs class.
To modify our code to use the first built-in delegate, we can delete our delegate and change our C# event to use the EventHandler delegate. When we raise the event, we must follow along with the delegate definition and pass in the required parameter values. Note how we pass our current instance of adder for the first parameter (sender) and since we are not passing back any event arguments, we use EventArgs.Empty for “e”.
We also had to change our event handler method to follow the pattern of the delegate with (object sender, EventArgs e).
Y2xhc3MgUHJvZ3JhbQp7CiAgc3RhdGljIHZvaWQgTWFpbigpCiAgewogICAgQWRkZXIgYSA9IG5ldyBBZGRlcigpOwogICAgYS5Pbk11bHRpcGxlT2ZGaXZlUmVhY2hlZCArPSBhX011bHRpcGxlT2ZGaXZlUmVhY2hlZDsKICAgIGludCBpQW5zd2VyID0gYS5BZGQoNCwgMyk7CiAgICBDb25zb2xlLldyaXRlTGluZSgiaUFuc3dlciA9IHswfSIsIGlBbnN3ZXIpOwogICAgaUFuc3dlciA9IGEuQWRkKDQsIDYpOwogICAgQ29uc29sZS5Xcml0ZUxpbmUoImlBbnN3ZXIgPSB7MH0iLCBpQW5zd2VyKTsKICAgIENvbnNvbGUuUmVhZEtleSgpOwogIH0KCiAgc3RhdGljIHZvaWQgYV9NdWx0aXBsZU9mRml2ZVJlYWNoZWQob2JqZWN0IHNlbmRlciwgRXZlbnRBcmdzIGUpCiAgewogICAgQ29uc29sZS5Xcml0ZUxpbmUoIk11bHRpcGxlIG9mIGZpdmUgcmVhY2hlZCEiKTsKICB9Cn0KCnB1YmxpYyBjbGFzcyBBZGRlcgp7CiAgcHVibGljIGV2ZW50IEV2ZW50SGFuZGxlciBPbk11bHRpcGxlT2ZGaXZlUmVhY2hlZDsKICAKICBwdWJsaWMgaW50IEFkZChpbnQgeCwgaW50IHkpCiAgewogICAgaW50IGlTdW0gPSB4ICsgeTsKICAgIGlmICgoaVN1bSAlIDUgPT0gMCkgJiYgKE9uTXVsdGlwbGVPZkZpdmVSZWFjaGVkICE9IG51bGwpKQogICAgeyBPbk11bHRpcGxlT2ZGaXZlUmVhY2hlZCh0aGlzLCBFdmVudEFyZ3MuRW1wdHkpOyB9CiAgICByZXR1cm4gaVN1bTsKICB9Cn0=
In order to use the other delegate and pass the grand total back to the event hander method, we first need to define a custom class called MultipleOfFiveEventArgs for passing back a custom value, such as Total. It must inherit from the EventArgs class.
Then we will need to define our event to use the other generic delegate which includes the custom EventArgs type, MultipleOfFiveEventArgs. We must also change how we raise the event. Finally, we change the event handler method to match the delegate.
Here is the complete code:
Y2xhc3MgUHJvZ3JhbQp7CiAgc3RhdGljIHZvaWQgTWFpbigpCiAgewogICAgQWRkZXIgYSA9IG5ldyBBZGRlcigpOwogICAgYS5Pbk11bHRpcGxlT2ZGaXZlUmVhY2hlZCArPSBhX011bHRpcGxlT2ZGaXZlUmVhY2hlZDsKICAgIGludCBpQW5zd2VyID0gYS5BZGQoNCwgMyk7CiAgICBDb25zb2xlLldyaXRlTGluZSgiaUFuc3dlciA9IHswfSIsIGlBbnN3ZXIpOwogICAgaUFuc3dlciA9IGEuQWRkKDQsIDYpOwogICAgQ29uc29sZS5Xcml0ZUxpbmUoImlBbnN3ZXIgPSB7MH0iLCBpQW5zd2VyKTsKICAgIENvbnNvbGUuUmVhZEtleSgpOwogIH0KCiAgc3RhdGljIHZvaWQgYV9NdWx0aXBsZU9mRml2ZVJlYWNoZWQob2JqZWN0IHNlbmRlciwgTXVsdGlwbGVPZkZpdmVFdmVudEFyZ3MgZSkKICB7CiAgICBDb25zb2xlLldyaXRlTGluZSgiTXVsdGlwbGUgb2YgZml2ZSByZWFjaGVkOiAiLCBlLlRvdGFsKTsKICB9Cn0KCnB1YmxpYyBjbGFzcyBBZGRlcgp7CiAgcHVibGljIGV2ZW50IEV2ZW50SGFuZGxlcjxNdWx0aXBsZU9mRml2ZUV2ZW50QXJncz4gT25NdWx0aXBsZU9mRml2ZVJlYWNoZWQ7CiAgcHVibGljIGludCBBZGQoaW50IHgsIGludCB5KQogIHsKICAgIGludCBpU3VtID0geCArIHk7CiAgICBpZiAoKGlTdW0gJSA1ID09IDApICYmIChPbk11bHRpcGxlT2ZGaXZlUmVhY2hlZCAhPSBudWxsKSkKICAgIHsgT25NdWx0aXBsZU9mRml2ZVJlYWNoZWQodGhpcywgbmV3IE11bHRpcGxlT2ZGaXZlRXZlbnRBcmdzKGlTdW0pKTsgfQogICAgcmV0dXJuIGlTdW07CiAgfQp9CgpwdWJsaWMgY2xhc3MgTXVsdGlwbGVPZkZpdmVFdmVudEFyZ3MgOiBFdmVudEFyZ3MKewogIHB1YmxpYyBNdWx0aXBsZU9mRml2ZUV2ZW50QXJncyhpbnQgaVRvdGFsKQogIHsgVG90YWwgPSBpVG90YWw7IH0KICBwdWJsaWMgaW50IFRvdGFsIHsgZ2V0OyBzZXQ7IH0KfQ==
Conclusion
I hope this undestanding C# events tutorial has been helpful for you in learning delegates and events. Best wishes and happy programming!
GET TO KNOW US
Davin Mickelson
Intertech Instructor
Davin is a lead instructor at Intertech and author of several blog posts on topics that include everything you would ever need to know about Microsoft software technologies.
“Whether you need help implementing Agile or need to boost your capabilities with a team of software developers that have proven expertise in design and development, no matter the technology, our proven consulting services can help you succeed the first time.”
Consulting Services
Consider introducing Intertech’s experience into your project and you will realize a higher degree of success around project goals, deliverables, timelines, and budgets.
Training Services
Consider Intertech for your companies training needs! A primary provider of training for fortune 5 to 5000 companies, Intertech brings a (1) proven resource that (2) knows how to plug into your system in a way that (3) accents your internal university system and makes you look great.
More Information To Consider
Xamarin – What Is It? And Creating A Blank App!
Custom Software Development And Is It Time To Consider Legacy Modernization?
The Fastest Way To Build Software Is “Right” The First Time!
Understanding your industry is one thing. Understanding the technology you are using is another. When you read studies that tell you that 75% of projects are doomed from the beginning, it has to make you pause before signing your name to the outcome.
Consider letting our proven professionals take a look at your project. They’ve seen what can go wrong and know how to avoid costly errors.
We build custom software from start to finish. We plug into your environment with the proven expertise you need for us to work independently or in co-development. And, we bring the soft-skills that make the task enjoyable, and the experience to leave your team stronger and ready to take over.
We Bring You…
Team-Complete™ Development
Soft-Skills For A Winning Experience
Sometimes the most critical person in the room is the one with a calm voice and the knowledge to select the right words. Bringing a development team together or presenting a clear concept for stakeholders can make all the difference between success or failure. Intertech consultants are at the top of their field. They navigate challenging decisions, guide with a confident voice, and know when to get out of the way.
Intertech takes the worry out of custom software development.
Let’s Build Something Great!
Tell us what you need and we’ll get back with you ASAP!
How Can We Help You?
Turn-Key Custom Application Development Solutions Since 1991
Independent & Co-Development

Tom Salonek Founder & CEO