Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Delegate Example
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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); } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,12 sec, absolute running time: 0,09 sec, cpu time: 0,09 sec, average memory usage: 14 Mb, average nr of threads: 3
edit mode
|
history
|
discussion
--BEFORE SORT-- 44 84 26 10 64 --AFTER SORT-- 84 64 44 26 10