Real Use Of Delegates In C#
Introduction
What is a delegate?
class program {
public delegate void somemethodptr()
static void Main(string[] args) {
somemethodptr obj = new somemethodptr(somemethod);
obj.Invoke();
}
static void somemethod() {
console.WriteLine(“Method called”);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Delegateexample
{
class Program
{
static void Main(string[] args)
{
myclass obj = new myclass();
obj.longrunningmethod(callback);
}
static void callback(int i)
{
Console.WriteLine(i);
}
public class myclass
{
public delegate void callback(int i);
public void longrunningmethod( callback obj)
{
for (int i = 0; i <= 100; i++)
{
// does something
obj(i);
}
}
}
}
}
Add comment