Events and Delegates in C#

Posted on Updated on

Events and Delegates in C#

Some of the tech guys face difficulties in differentiating between events and delegates in C#. This post will help you know the basic difference between both. In programming we often faced some conditions where we want to run a particular action but even we don’t know earlier that which object or which subroutine we going to execute. For example, if you have some media player, you don’t know which type of song it will run like it can be a gif, mpeg, mp4 or any audio file. So rather than again and again converting your song into machine code one can simply create a delegate.

In the early languages we have to follow code and constructs of the programming language so if we created a program for addition of two integers using a function, then same function cannot be used to add two numbers of float type. That the concept you followed in traditional languages like C and C++ but not yet followed by console programming.

But now the tradition have been changed as GUI appears and so this programming known as event- driven programming. In this type of interface user have various options to do it can be selecting menu, clicking buttons or etc. Each action of the user raise an event. Some events may triggered on the tick of a clock, on reading writing a file and so on.

An event means something happened in the program. Events are delegates are dependent upon each other. But we need dynamic event handling which means program interact in the way as user approaches the program. An Event handler implemented using delegates in C#.

Delegates

It is like a type of reference as such as you have seen in most of the books, Wikipedia etc. Delegates refers to a procedure rather than an object. When we create a delegate, we must provide a name and return type; once we provided we can encapsulate (include) any method inside the delegate.

For creating a delegate we need a keyword delegate. Then to provide its RETURN TYPE and then event prototype.

Example:

Public delegate int display (object o, obj o2);

This above declaration creates a delegate named display, which will handle any method that have same signature as that of delegate. Once it is defined, you can capture a member function with that delegate by creating an instance of the delegate. One important thing here to note is that delegates have no body. Because we are not going to define method here.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Example_17_1_ _ _ _Using_Delegates

{

public class MediaStorage

{

public delegate int PlayMedia( );

public void ReportResult(PlayMedia playerDelegate)

{

if (playerDelegate( ) == 0)

{

Console.WriteLine(“Media played successfully.”);

}

else

{

Console.WriteLine(“Media did not play successfully.”);

}

}

}

public class AudioPlayer

{

private int audioPlayerStatus;

public int PlayAudioFile( )

{

Console.WriteLine(“Simulating playing an audio file here.”);

audioPlayerStatus = 0;

return audioPlayerStatus;

}

}

public class VideoPlayer

{

private int videoPlayerStatus;

public int PlayVideoFile( )

{

Console.WriteLine(“Simulating a failed video file here.”);

videoPlayerStatus = -1;

return videoPlayerStatus;

}

}

public class Tester

{

public void Run( )

{

MediaStorage myMediaStorage = new MediaStorage( );

// instantiate the two media players

AudioPlayer myAudioPlayer = new AudioPlayer( );

VideoPlayer myVideoPlayer = new VideoPlayer( );

// instantiate the delegates

MediaStorage.PlayMedia audioPlayerDelegate = new

MediaStorage.PlayMedia(myAudioPlayer.PlayAudioFile);

MediaStorage.PlayMedia videoPlayerDelegate = new

MediaStorage.PlayMedia(myVideoPlayer.PlayVideoFile);

// call the delegates

myMediaStorage.ReportResult(audioPlayerDelegate);

myMediaStorage.ReportResult(videoPlayerDelegate);

}

}

class Program

{

static void Main(string[] args)

{

Tester t = new Tester( );

t.Run( );

}

}

}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s