using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Apps
{
class DelegateWithInstanceMethod
{
private int count;
public DelegateWithInstanceMethod(int count)
{
this.count = count;
}
public void Print(string message)
{
Console.WriteLine(message);
Console.WriteLine(count);
}
}
class UnderstandingDelegates
{
public delegate void PrintValue(string message);
public static void Main()
{
//create delegate and make it point ot method
PrintValue d = new PrintValue(Print); // Example 1
//invoke method pointed by delegate
d("Hello - Normal delegate call");
d.Invoke("Hi");
//Method group conversion
PrintValue d2 = Print; // Example 2
//invoke method pointed by delegate
d2("Nice - Method group conversion");
d2.Invoke("Good");
//Using delegate With Instance Method
DelegateWithInstanceMethod s = new DelegateWithInstanceMethod(10);
//Method group conversion with instance method
PrintValue d3 = s.Print; // Example 3
//invoke method pointed by delegate
d3.Invoke("Invoked the delegate using the instance method");
}
public static void Print(string message)
{
Console.WriteLine(message);
}
}
}
No comments:
Post a Comment