using System;
using ;
using ;
namespace DelegateAndEvent.App_Code
{
public class Common
{
//Define global variables.
public static string txt = "";
#region Definition Method
public string HelloCSharp(string name)
{
txt += "hello " + name;//This is done to see that the delegate can execute multiple methods.
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region Definition
//Defining a delegate is similar to defining a method, the difference is to add a delegate. Remove the method body and only write the method signature.
public delegate string SayHi(string name);
//Delegates can be used like ordinary variables. The difference is that multiple methods can be assigned to delegates.
public SayHi dlgt1, dlgt2;
//Use Delegation
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region Event
//Declare the event
public event SayHi hiEvent;
//Trigger event
public void causeEvent()
{
hiEvent += HelloCSharp;
hiEvent += HiCSharp;
if (hiEvent != null)
{
hiEvent("crane");
}
}
#endregion
}
}
using System;
using ;
using ;
namespace DelegateAndEvent.App_Code
{
public class Common
{
//Define global variables.
public static string txt = "";
#region Definition Method
public string HelloCSharp(string name)
{
txt += "hello " + name;//This is done to see that the delegate can execute multiple methods.
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region Definition
//Defining a delegate is similar to defining a method, the difference is to add a delegate. Remove the method body and only write the method signature.
public delegate string SayHi(string name);
//Delegates can be used like ordinary variables. The difference is that multiple methods can be assigned to delegates.
public SayHi dlgt1, dlgt2;
//Use Delegation
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region Event
//Declare the event
public event SayHi hiEvent;
//Trigger event
public void causeEvent()
{
hiEvent += HelloCSharp;
hiEvent += HiCSharp;
if (hiEvent != null)
{
hiEvent("crane");
}
}
#endregion
}
}