What is a delegate in c# and why do we are use delegate in C# programming?
What is a delegate in c# and why do we are use delegate in C# programming?
Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur.
SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer
Ashutosh Kumar Verma
24-Aug-2021Delegate 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));
}
}