Delegate in c# C# delegate is like reference type variable which holds reference of a method. In runtime, this reference can change. All delegate is derived from System.Delegate class. 'delegate' keyword is used to declare a delegate.
In delegate we can pass function like a parameter. To holds the reference of any method by delegate which has same return type and parameter as like delegate. In other word 'we can use delegate to hold the reference of that method which has same return type and parameter as delegate has.'
example
using System;
class Example
{ public delegate void Demo(int a, int b); //declare a delegate.
static void main(String arg[])
{
Demo d= Add;
d(10,20);
Console.ReadLine();
}
public static void Add(int x, int y) // method has same return type and parameter
{
Console.WriteLine('Sum is '+ (x+y));
}
}
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Delegate in c# C# delegate is like reference type variable which holds reference of a method. In runtime, this reference can change. All delegate is derived from System.Delegate class. 'delegate' keyword is used to declare a delegate.
syntax of delegate in c#
<access modifier> delegate <return type> <delegate_name>(<parameters>)
Use of delegate
In delegate we can pass function like a parameter. To holds the reference of any method by delegate which has same return type and parameter as like delegate. In other word 'we can use delegate to hold the reference of that method which has same return type and parameter as delegate has.'
example
using System;
class Example
{ public delegate void Demo(int a, int b); //declare a delegate.
static void main(String arg[])
{
Demo d= Add;
d(10,20);
Console.ReadLine();
}
public static void Add(int x, int y) // method has same return type and parameter
{
Console.WriteLine('Sum is '+ (x+y));
}
}