Sunday, November 10, 2013

C# event – Understanding c# events – part 1

Event
– a noun – described as “something that happens, especially one of importance”.

Example
– The formula 1 race was an amazing event
to see.

C#
events are no different from description stated above. The only difference
being “Something happens on - Objects”. Any c# language feature can be well
understood with real life example only. C# being OOP based language; let’s
consider a real life example.

Let’s
consider a popular game of Cricket to understand C# events.

Suppose
you are developing a wonderful game of cricket for cricket lovers. Assume that
this game is going to shake the world of gaming and you will be earning
billions with the popularity of this game. At first to develop basic version
you need Ball, Fielder, and Umpire and Fan (the person who is crazy about
cricket) objects. Here are the responsibilities of every object –

Ball
– Lets fielder know that I am in the field and catch me.

Fielder
– fields or catches the ball when ball is in the ground.

Umpire
– Watches the activity when Ball is in the ground and responds accordingly.

Fan –
Watches the activities happening on the Ball and yells, cheers up, celebrates
and many more!!!

Now
everything is in place now you just need to connect everything together. So
following diagram illustrates what we wish to do –

However
there is problem because Ball does not know which fielder out of 11 present on
the ground will caught or grab him. Similarly Fielder only needs to know about
fielding or catching a ball. Hence Ball should only get hit and should not know
about the fielder; whereas fielder should only know about catching the ball.

In short – We want
every object to know about it and not others. Hence we wish to separate the
concerns of each object.

When
the ball is hit we will use an EVENT to let fielders know about it. So those,
our fielders can respond to the event of ball. Similarly we can make other
objects like Umpire, Fan to know about Ball event so that they can also respond
to it. So we want Ball object to raise
an event. Then we will have other
objects subscribed to that type of Ball event. When event is raised the subscribed
objects will be notified and they can take the action the way they want. This action to be taken when an event is raised,
written in Event Handler. The event handler code is executed every time the
event is raised. It is just a method in the subscriber object which runs every
time event is raised.

Event Arguments – or Event
Args –

When
ball is hit and it is in the sky the fielder has to take judgement based on
height and distance of the ball, its position, time required to reach to the
point ball touching the ground. He has to take decision based on these
parameters whether he will catch it or just field it. This decision or in other
words handling of event BallInGround depends on Ball object’s height and
distance. Here Ball’s height and distance is nothing but Event arguments which
provide more information about the event that rose.

In Short - When
event is raised, it is handled in event Handler method. However event handler
method always needs information about events based on which handler method can
take action. This is provided in Event Arguments.

Overall Summary and
steps about c# events and event handlers- - Objects subscribe to the event.-Some
action triggers an event.- Event
is raised.- Subscribers
get notifications about raised event.-Each object who has subscribed to the event handles
in its own way.

Followers

About Me

I am Kunal Chandratre. Working as Cloud Solution Architect @Microsoft. My speciality is Microsoft Azure Cloud platform.
Awarded as Most Valuable Professional (MVP) in Microsoft Azure for consecutive 3 years. Passionate speaker, trainer...In free time (which I don't get usually)I write blogs and answers the forum questions. I was doing it just for timespass but now I have got addicted to blogging...Apart from work, I do variety of things which I can't tell here:).. I am trekker, singer, actor, painter, f1 racer, super hero in my dreams.. ...and now trying my luck with technologies...Keep posting...

Visitors

Disclaimer:

The information shared in this blog is the result of my personal experience with various technology platforms. In no way it represents the company I work for.
The information provided here is "AS IS" with no warranties, and confers no rights. This blog does not represent the thoughts, intentions, plans or strategies of my current employer or past empolyers or any other forums or community I belong to. It is fully my own opinion. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.I have full rights to edit/modify/delete any content of this blog without any prior notice to public/followers/RSS readers of this blog.