Delegates , Events , Publishing , subscribing

ron naber
.NET voyage
Published in
4 min readNov 3, 2014

--

this is something new I have tried here , all explanations are in comments. As they say in OOP , have smart coupling…

Just copy the below code in your visual studio :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

namespace _2112014

{

class Program

{

static void Main(string[] args)

{

#region Delegate,action

//multicast delegate example go to UseDelegate class to see implemenation

//UseDelegate UD = new UseDelegate(4, 5);

//calling action delegate , action delegate is kept outside function

//UseDelegate UD = new UseDelegate();

//UD.MyAction(5, 5);

#endregion

#region eventhandler example on filesystem

// event handler is delegate which sits between suscriber and publisher

// just mouse over the event -> watcher.created above and see what kind of event handler it excepts

// check out the parameter

// and create same signature method in same class or other class and pass the method

//var watcher = new FileSystemWatcher(“C:\\Users\\rohan\\Desktop\\Event”);

//watcher.EnableRaisingEvents = true;

//watcher.Created += new FileSystemEventHandler(OnCreated);

//watcher.Deleted += OnDeleted;

//watcher.Changed += OnChanged;

#endregion

#region Event suscribing Car class event

//Car C = new Car(); //creating instance of that publisher class — Car

//C.change += ReturnWarningSign;//suscribing to event , telling if this event occurs then run this function

//C.pspeed = 89; //making the event fire

#endregion

// Console.ReadLine(); //so program doesnot close console screen

}

//public static void ReturnWarningSign() //fire this event when event occurs in publisher class

//{

// Console.WriteLine(“reduce speed”);

// Console.ReadLine();

//}

#region EventFunctions

private static void OnChanged(object sender, FileSystemEventArgs e)

{

Console.WriteLine(“{0} : {1} “, e.ChangeType, e.Name);

}

static void OnDeleted(object sender, FileSystemEventArgs e)

{

Console.WriteLine(“{0} : {1} “, e.ChangeType, e.Name);

}

static void OnCreated(object obj, FileSystemEventArgs e)

{

Console.WriteLine(“{0} : {1} “, e.ChangeType, e.Name);

}

#endregion

}

}

same namespace

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace _2112014

{

public class UseDelegate

{

// TODO: Complete member initialization

//action and lamda expression

//action and func are types of delegates , action is used when you dont know exact signature

//action accepts 16 input parameters and no return types , while func can have return type

//lamda expression => is used to make functions on fly like below example , please study the syntax

public Action<int, int> MyAction = (int a, int b) => { Console.WriteLine(a + b); };

public UseDelegate()

{

}

public UseDelegate(int x , int y)

{

//creating multicast deligate — a deligate which contains more than one function

//# region mathematical function

Calculate MulticastCalculateDelegateObject = Add;

MulticastCalculateDelegateObject += Subtract;

MulticastCalculateDelegateObject += Divide;

MulticastCalculateDelegateObject += Multiply;

MulticastCalculateDelegateObject(x, y);

}

public delegate string Calculate(int x , int y);

#region MathematicalFunction

public string Add(int x, int y)

{

return ((x + y).ToString());

}

public string Multiply(int x, int y)

{

return ((x * y).ToString());

}

public string Divide(int x, int y)

{

return ((x / y).ToString());

}

public string Subtract(int x, int y)

{

return ((x — y).ToString());

}

#endregion

}

}

public class UseDelegate

{

// TODO: Complete member initialization

//action and lamda expression

//action and func are types of delegates , action is used when you dont know exact signature

//action accepts 16 input parameters and no return types , while func can have return type

//lamda expression => is used to make functions on fly like below example , please study the syntax

public Action<int, int> MyAction = (int a, int b) => { Console.WriteLine(a + b); };

public UseDelegate()

{

}

public UseDelegate(int x , int y)

{

//creating multicast deligate — a deligate which contains more than one function

//# region mathematical function

Calculate MulticastCalculateDelegateObject = Add;

MulticastCalculateDelegateObject += Subtract;

MulticastCalculateDelegateObject += Divide;

MulticastCalculateDelegateObject += Multiply;

MulticastCalculateDelegateObject(x, y);

}

public delegate string Calculate(int x , int y);

#region MathematicalFunction

public string Add(int x, int y)

{

return ((x + y).ToString());

}

public string Multiply(int x, int y)

{

return ((x * y).ToString());

}

public string Divide(int x, int y)

{

return ((x / y).ToString());

}

public string Subtract(int x, int y)

{

return ((x — y).ToString());

}

#endregion

}

same namespace

public class Car

{

public Car()

{

}

//created to learn about creating event , firing event , suscribing event to other class

//logic if speed greater than 50 then fire event

//declare an event

public event Action change;

private double speed;

public double pspeed

{

get {

return speed;

}

set { speed = value;

if (speed>50)

{

if (change != null)

{

change(); //fire the event if the condition satisfies

}

}

}

}

}

--

--

ron naber
.NET voyage

writing sets you free , lets you explore and enjoy life more, so read n write