Delegate Types in C#
Delegate, a keyword, is used to create method objects. With the Invoke method we can call a delegate as a method. And we can pass the delegate as an argument to another method. A delegate is a higher-order procedure.
Example. This program declares a delegate type D that receives a string, and returns no value. In Main, we create an instance of type D. We specify the method body with a lambda expression.
Example. This program declares a delegate type D that receives a string, and returns no value. In Main, we create an instance of type D. We specify the method body with a lambda expression.
Then:We call the delegate D method implementation with the argument "cat" using the Invoke method. This is appropriate for the Internet.
Tip:This program uses the "delegate" keyword. But many programs use higher-order procedures with just lambda expressions.
Based on: .NET 4.5 C# program that uses delegate using System; class Program { delegate void D(string value); static void Main() { // ... Specify delegate with lambda expression. D d = v => Console.WriteLine(v); // ... Invoke delegate. d.Invoke("elephent"); } } Outputelephent
No comments: