Prerequisite:
- Type Safe: Can not move one data type to other data type. Type safety means preventing type errors. Type error happens when unknowingly developer assigns one data type to other causing bugs. In C# you can not assign one type value to another type value. C# is type safe, JavaScript is not type safe.
- Conversion: Converts from one data type to other data type using the convert class.
- Casting: Converts from one data type to other data type by defining the data type.
- Implicit Casting: No need to define to cast from where to where. No data loss.
- Explicit Casting: Need to define to cast from where to where. Data loss can happen.
- Reference Type: Unlike value types, a reference type doesn't store its value directly. Instead, it stores the address where the value is being stored. In other words, a reference type contains a pointer to another memory location that holds the data.
Delegate:
- Type safe function pointer.
- Just like class and string, Delegate is also reference type.
- Representative for the communication between two parties.
- Real world use is to callback and do the data communication.
Example-1:
public class DelegateDemo
{
public delegate void DelegateName1();
public delegate int DelegateName2();
public delegate bool DelegateName3();
public delegate bool DelegateName4(int x);
public void method1()
{
Console.WriteLine("Method1 called.");
}
public int method2()
{
Console.WriteLine("Method2 called.");
return 0;
}
private bool method3()
{
Console.WriteLine("Method3 called.");
return false;
}
private bool method4(int x)
{
Console.WriteLine("Method4 called.");
return false;
}
public void method5()
{
Console.WriteLine("Method5 called.");
}
public void Demo()
{
// calling methods directly
method1();
method2();
method3();
method5();
// calling methods using reference i.e. delegate
Console.WriteLine("Calling from delegate");
DelegateName1 delegateName1 = new DelegateName1(method1);
delegateName1();
delegateName1.Invoke();
delegateName1 += method5;
delegateName1();
DelegateName2 delegateName2 = new DelegateName2(method2);
DelegateName3 delegateName3 = new DelegateName3(method3);
DelegateName4 delegateName4 = new DelegateName4(method4);
delegateName2();
delegateName3();
delegateName4(0);
}
}
Output:
Example-2:
Business Class
public class DelegateDemoBusinessClass
{
public delegate void DelGetCalculatedValue(int x, int y);
private int _x, _y;
private DelGetCalculatedValue _delegateObject;// = new DelGetCalculatedValue(getCalculatedValue);
public DelegateDemoBusinessClass(int x, int y, DelGetCalculatedValue getCalculatedValue)
{
_x = x;
_y = y;
_delegateObject = this.getCalculatedValue;
}
public void getCalculatedValue(int x, int y)
{
_delegateObject(x, y);
}
}
User (Caller) Class
public class DelegateDemoCallerClass
{
enum typeOperation { Addition = 0, Substraction = 1, Multiplication = 2, Division = 3 };
public static void Demo()
{
int x = 20, y = 10, operationChoice = 3;
DelegateDemoBusinessClass.DelGetCalculatedValue getCalculated;
if (operationChoice == Convert.ToInt32(typeOperation.Addition))
{
getCalculated = myAdd;
}
else if (operationChoice == Convert.ToInt32(typeOperation.Substraction))
{
getCalculated = mySub;
}
else if (operationChoice == Convert.ToInt32(typeOperation.Multiplication))
{
getCalculated = myMul;
}
else
{
getCalculated = myDiv;
}
DelegateDemoBusinessClass demobusinessclass = new DelegateDemoBusinessClass(x, y, getCalculated);
getCalculated(x, y);
}
public static void myAdd(int x, int y)
{
Console.WriteLine(x + y);
}
public static void mySub(int x, int y)
{
Console.WriteLine(x - y);
}
public static void myMul(int x, int y)
{
Console.WriteLine(x * y);
}
public static void myDiv(int x, int y)
{
Console.WriteLine(x / y);
}
}
Multicast Delegate:
- That has references to more than one function.
- Two approaches to create multicast delegate.
- + or += to register a method with the delegate
- - or -= to un-register a method with the delete
- Invokes the methods in the invocation list, the the same order in which they are added.
- If the delegate has a return type other than void and if the delegate is a multicast delegate, only the value of the last invoked method will be returned.
- If the delegate has an out parameter, the value of the output parameter, will be the value assigned by the last method.
- Usage: Observer design pattern.