Menu
Visual Studio guides you with live assistance as you write Visual Basic and C# code through IntelliSense and Analyzers, as well as provides world-class debugging and diagnostics tools. Learn about editing in Visual Studio. Learn about debugging in Visual Studio. Microsoft.NET – What Does It Mean To You? PDC 2000 Build. Visual Studio 1.0 Beta 1. November, 2000. Book began in January, 2001. Visual Studio 1.0 Beta 2. First Production ASP.NET App in July, 2001. Production Windows Service in November, 2001. Documents Similar To Why Dot Net.
Welcome to
Introducing a new approach to learning
The skills required to advance your career and earn your spot at the top do not come easily. Now there’s a more rewarding approach to hands-on learning that helps you achieve your goals faster. Earn points, levels, and achieve more!
Start learning today
Up your game with a module or learning path tailored to today's developer and technology masterminds and designed to prepare you for industry-recognized Microsoft certifications.
Hands-on learning
Master core concepts at your speed and on your schedule. Whether you've got 15 minutes or an hour, you can develop practical skills through interactive training.
Learn for free
When we say free, we mean it. Instant in-browser access to Microsoft tools and modules. No credit card required.
Learn Azure
Explore more advanced Azure topics with online courses.
Additional Azure training
Explore more ways to learn Azure with self-paced video courses provided free in partnership with Pluralsight and Microsoft Hands-on-labs to practice what you learn.
Learn Dynamics
Microsoft Dynamics 365 is a set of applications that are connected by data and intelligence. Use Dynamics 365 to unleash your organization’s potential.
Additional training
Partner eLearning resources to meet your learning needs and take your skills to the next level.
Learn Power Platform
Use PowerApps, Power BI, and Microsoft Flow to customize, extend, and build all the apps you need for your business and unlock the potential of Office 365 and Dynamics 365.
Additional training
Partner eLearning resources to meet your learning needs and take your skills to the next level.
Learn Windows Development
Start developing Windows applications.
Microsoft Learn is just the beginning
Start with the basics, then move to advanced methods that address real-world challenges. Microsoft Learn meets you where you are and takes you where you want to go.
Certifications
Jump-start your career and demonstrate your achievements through industry-recognized Microsoft certifications.
Instructor-led training
Sharpen your technology skills with in-depth, on-demand training in virtual classrooms led by Microsoft Certified Trainers.
More learning
Choose from a wide range of programs designed to fit your learning needs and help you take your IT skills and your career to the next level.
Aka Microsoft Partner
16 Oct 2001CPOL
An introduction to using events and event handlers in C#
Introduction
![Bot Bot](/uploads/1/2/4/8/124851300/941157136.jpg)
An event is a mechanism via which a class can notify its clients whensomething happens. For example when you click a button, a button-click-eventnotification is sent to the window hosting the button. Events are declared usingdelegates. So if you don't know what a delegate is, you may go and read myarticle on delegates first. The first time you go through this article you mightfind it confusing, don't worry about that. Just try out the sample program andgo through the sample source, line by line. Maybe, you can read the article once moreafter that. Once you get the hang of it, things will seem simple. By the waythis article is intended for beginners and is not meant for advanced levelprogrammers.
The Program
I include the full program below. It's not commented but later down thisarticle, I have taken the program part by part and explained each part. I havealso included the output you'll get on running the program.
Explanation
Okay. I presume you have taken a look at the above program and I bet you have allguessed it's purpose. We generate some numbers and every time we generate anumber that is divisible by 7 we raise an event. The event handler will printout a message saying that the event was raised and it also prints out the numberresponsible for raising the event. I guess some of you are frowning and sayingthat's a stupid reason to raise an event. I know, I know alright. The program isnot intended to be used for any useful purpose. It's only an attempt to makeevents comprehensible. I hope it served it's role out. :-)
Okay, so the first thing we did was to declare a delegate.
The delegate defines the parameters sent to the event handlers. Thus anyclass that wants to handle this event must have a handler method which has thesame return type and argument list as this delegate. Here as you can see, thefirst parameter is an object. In real-world cases event handlers are normallypassed a reference to the sending object. I am not doing that in this program. Iam simply passing a
new object()
to the event handler. Normally youcan pass a this
reference. The second parameter is a System.EventArgs
derived class. System.EventArgs
is the base class for encapsulating event related data.We use it to send information regarding the event to its handler.Now, we define the
EventArgs
derived class as follows:-As you can see, it has a public read-only member which is used to store ourgenerated number that is divisible by 7. Normally you should use properties butfor the sake of simplicity I am using a public member variable.
Now we define our listener class which is the class that needs to be notifiedof the event.
As you can see, it has a function
ShowOnScreen
that matches the delegate typewe defined on top. You can see how we use the passed DivBySevenEventArgs
objectto print out the number that is divisible by seven.Now, let's examine our
Main()
containing class. We first declare the event asfollows:-An event is declared like a delegate type variable, except that the keyword
event
precedes the event declaration.Now let's take a look at the function that invokes the event and thusnotifies all clients.
Aka Microsoft Bot Framework
EventSeven
will be null
if no client has hooked up a delegate to the event.We need to check if it is null
unless you want to see an exception raised. Ifit's not null
we invoke the event, passing a dummy object [I have explainedabove why I passed a dummy] and the passed DivBySevenEventArgs
object. And allclients get notified.Aka Microsoft Dynamics
Let's look at the function
GenNumbers()
now :-We use the
for()
loop to iterate from 0 to 98, and in each case we check fordivisibility by 7. If we find that the number is divisible by 7 we create aDivBySevenEventArgs
object passing the culprit number to the constructor. Now wecall the OnEventSeven()
function passing the DivBySevenEventArgs
object we justcreated. Now lets go through
Main()
We first create a
DivBySevenListener
object. Then using the +=
operator we compose a delegate into the event field. Even though I haven't usedthe -=
operator, you can use it to remove a delegate froman event. Once we've done all that we call GenNumbers()
. Thus we've seteverything up nice and proper. GenNumbers()
will do its job like a faithfulpuppy by generating numbers from 0 to 98 just as we expected it to. Every timeit generates a number divisible by 7 it will raise the event. Conclusion
Well you have just seen how you can create events and event handlers. Youmust remember that events can be invoked only from the class that declared them.This causes problems with regard to inheritance. Thus if you have a class withan event you'd better make your OnWhateverEvent() function protected so that thederived classed can call it. Better still, make it virtual too, so they canoverride it in their class.