Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

delegates in c#

//A delegate is an object that knows how to call a method.
delegate int Transformer (int x);
//Transformer is compatible with any method 
//with an int return type and a single int parameter,
int Square (int x) 
{ 
  return x * x; 
}
//Or, more tersely:
int Square (int x) => x * x;
//Assigning a method to a delegate variable creates a delegate instance:
Transformer t = Square;
//You can invoke a delegate instance in the same way as a method:
int answer = t(3);    // answer is 9
Source by learning.oreilly.com #
 
PREVIOUS NEXT
Tagged: #delegates
ADD COMMENT
Topic
Name
6+2 =