Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Delegate Example
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 //DELEGATE EXAMPLE using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { Random rand = new Random(); int i = rand.Next(0, 100); ClassA DecisionClass = new ClassA(i); ClassB SortingClass = new ClassB(DecisionClass.GetCurrentSorter()); Console.WriteLine("--BEFORE SORT--"); SortingClass.PrintList(); SortingClass.SortTheList(); Console.WriteLine("--AFTER SORT--"); SortingClass.PrintList(); } } public class ClassA{ private ClassB.Sorter sort; public ClassA(int i){ if(i >= 50){ sort = SortA; } else{ sort = SortB; } } public ClassB.Sorter GetCurrentSorter(){ return sort; } public void SortA(List<int> numbers){ numbers.Sort((i1, i2) => {return i1.CompareTo(i2);} ); } public void SortB(List<int> numbers){ numbers.Sort((i1, i2) => {return i2.CompareTo(i1);} ); } } public class ClassB{ public delegate void Sorter(List<int> numbers); private Sorter sorter; private List<int> numbers = new List<int>(); public ClassB(Sorter sortFunc){ sorter = sortFunc; Random rand = new Random(); for(int i = 0; i < 5; i++){ numbers.Add(rand.Next(0, 100)); } } public void PrintList(){ foreach(int num in numbers){ Console.WriteLine(num); } } public void SortTheList(){ sorter(numbers); } } }
run
|
edit
|
history
|
help
0
Lambda Expression
Examples of JS-Like higher-order functions in C#
Fibonacci Series
Punnet Square Generator
isbn
123
Get days from two dates
Interface with same methods
abc
Clone Example