Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
3. Delegates and events
//3. Delegates and events using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { //Delegate is a type that can point to a function with certain signature. delegate bool TestPointer(int a); //Instances of a TestPointer can point to any function that takes int and returns bool //An event is a special method that can be subscribed to and fired static event TestPointer SomeEvent; //You can only subscribe functions that take int and return bool to this event. This is declared by using TestPointer delegate public static void Main(string[] args) { TestPointer test_function = f => f > 5; //Same as: //TestPointer test_function = f => //{ // return f > 5; //}; Console.WriteLine(test_function(6)); SomeEvent += SomeFunction; //+= means subscribe to this event, -= is unsubscribe SomeEvent += f => {Console.WriteLine("Hey 2! " + f); return true;}; //another one SomeEvent(25); //fire! All subscribees will be called in the order they subscribed. } public static bool SomeFunction(int a) { Console.WriteLine("Hey! " + a); return true; } } }
run
|
edit
|
history
|
help
0
Período
Chris Kasongo Lazarius Mukengeshayi "Entelect BlackJack 21"
Bubble sort
factorial ncr + unique list
sdefuy6j87uytgraxsd
2.2 Basic types: Dictionary
binary
nums y lists
fun with yield return
salary