What is delegate in .NET?

Forums .NETWhat is delegate in .NET?
Staff asked 3 years ago

Answers (1)

Add Answer
Umang Ramani Marked As Accepted
Staff answered 3 years ago

A delegate is a type in the.NET environment that defines a method signature and can provide a function as a parameter. To put it simply, a delegate is a. NET object that links to a method that meets its specific signature.

 

public delegate void MyDelegate(string msg); // declare a delegate

// set target method
MyDelegate del = new MyDelegate(MethodA);
// or 
MyDelegate del = MethodA; 
// or set lambda expression 
MyDelegate del = (string msg) =>  Console.WriteLine(msg);

// target method
static void MethodA(string message)
{
    Console.WriteLine(message);
}

 

Subscribe

Select Categories