mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-18 04:42:32 +03:00
Merge pull request #2946 from ksami/ksami-csharp-delegates
[csharp/en] #2314 Add delegates and events to csharp/en
This commit is contained in:
commit
4a40fa210a
@ -640,6 +640,54 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// DELEGATES AND EVENTS
|
||||||
|
public class DelegateTest
|
||||||
|
{
|
||||||
|
public static int count = 0;
|
||||||
|
public static int Increment()
|
||||||
|
{
|
||||||
|
// increment count then return it
|
||||||
|
return ++count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A delegate is a reference to a method
|
||||||
|
// To reference the Increment method,
|
||||||
|
// first declare a delegate with the same signature
|
||||||
|
// ie. takes no arguments and returns an int
|
||||||
|
public delegate int IncrementDelegate();
|
||||||
|
|
||||||
|
// An event can also be used to trigger delegates
|
||||||
|
// Create an event with the delegate type
|
||||||
|
public static event IncrementDelegate MyEvent;
|
||||||
|
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
// Refer to the Increment method by instantiating the delegate
|
||||||
|
// and passing the method itself in as an argument
|
||||||
|
IncrementDelegate inc = new IncrementDelegate(Increment);
|
||||||
|
Console.WriteLine(inc()); // => 1
|
||||||
|
|
||||||
|
// Delegates can be composed with the + operator
|
||||||
|
IncrementDelegate composedInc = inc;
|
||||||
|
composedInc += inc;
|
||||||
|
composedInc += inc;
|
||||||
|
|
||||||
|
// composedInc will run Increment 3 times
|
||||||
|
Console.WriteLine(composedInc()); // => 4
|
||||||
|
|
||||||
|
|
||||||
|
// Subscribe to the event with the delegate
|
||||||
|
MyEvent += new IncrementDelegate(Increment);
|
||||||
|
MyEvent += new IncrementDelegate(Increment);
|
||||||
|
|
||||||
|
// Trigger the event
|
||||||
|
// ie. run all delegates subscribed to this event
|
||||||
|
Console.WriteLine(MyEvent()); // => 6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Class Declaration Syntax:
|
// Class Declaration Syntax:
|
||||||
// <public/private/protected/internal> class <class name>{
|
// <public/private/protected/internal> class <class name>{
|
||||||
// //data fields, constructors, functions all inside.
|
// //data fields, constructors, functions all inside.
|
||||||
|
Loading…
Reference in New Issue
Block a user