CHAPTER 26

image

Delegates

A delegate is a type used to reference a method. This allows methods to be assigned to variables and passed as arguments. The delegate’s declaration specifies the method signature to which objects of the delegate type can refer. Delegates are by convention named with each word initially capitalized followed by “Delegate” at the end of the name.

delegate void MyDelegate(string s);

A method that matches the delegate’s signature can be assigned to a delegate object of this type.

class MyClass{   static void Print(string t)  {     System.Console.Write(t);  }     static void Main()  {     MyDelegate d = Print;  } } 

This delegate ...

Get C# Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.