static void DoForAll(IntAction action, int[] integers) { foreach (int i in integers) { action(i); } }
public static void ForEach(IEnumerable <int> values, IntAction action) { foreach (int value in values) { action.Invoke(value); } }
public static void Perform(IntAction act, params int[] arr) { foreach (int ele in arr) { act(ele); } }
public void Act(IntAction <T> f) { foreach (var i in this) { f(i); } }
public static void Perform(IntAction act, params int[] arr) { foreach (int i in arr) { act(i); } }
//modified method which accepts a variable number of parameters public static void PerformDifferently(IntAction intAct, params int[] numbers) { foreach (var number in numbers) { intAct(number); } }
static void Perform(IntAction act, int[] arr) { foreach (int item in arr) { act(item); } }
public void Act(IntAction f) { foreach (int i in this) { f(i); } }
public static void Main() { Console.WriteLine("Yield demo:"); GalaxyClass.ShowGalaxies(); Console.WriteLine("\n"); Console.WriteLine("Delegate demo:"); MyDelegate.RunDelegates(); Console.WriteLine("\n"); // Exercise 1 Console.WriteLine("Exercise 1:"); IntAction a = x => TestDelegate.PrintInt(x); a(42); int[] arr = { 3, 4, 14, 53, 54, 89, 11 }; TestDelegate.Perform(a, arr); TestDelegate.Perform(Console.WriteLine, arr); TestDelegate.Perform(Console.WriteLine, 0, 1, 4, 8, 16, 32); Console.WriteLine("\n"); // Exercise 2 Console.WriteLine("Exercise 2:"); Book[] myBooks = { new Book("Peter Sestoft", "Programming Language Concepts", 2012), new Book("Walt Disney", "Anders And", 1983), new Book("Peter Sestoft & Henrik I. Hansen", "C# Precisely (Second Edition)", 2012), new Book("Raghu Ramakrishnan & Johannes Gehrke", "Database Management Systems", 2003), new Book("David Basin, Patrick Schaller & Michael Schläpfer", "Applied Information Security", 2011), new Book("Y. Daniel Liang", "Introduction to Java Programming (Brief Version)", 2015), new Book("Lars Mathiassen, Andreas Munk-Madsen, Peter Axel Nielsen & Jan Stage", "Object Oriented Analysis & Design", 2000), new Book("Ian Sommerville", "Software Engineering", 2016), new Book("Susanna S. Epp", "Discrete Mathematics with Applications", 2011) }; myBooks = GenericMethods.Filter(myBooks, x => x.year < 2015); GenericMethods.Quicksort( myBooks, (x, y) => MyComparer.BookCompare( x, y, (bookA, bookB) => bookA.year - bookB.year, (bookA, bookB) => string.Compare(bookA.title, bookB.title), (bookA, bookB) => string.Compare(bookA.author, bookB.author))); foreach (var book in myBooks) { Console.WriteLine(book); } Console.WriteLine("\n"); String[] bookTitles = GenericMethods.Map <Book, String>(myBooks, x => x.title); foreach (var bookTitle in bookTitles) { Console.WriteLine(bookTitle); } Console.WriteLine("\n"); }
public static void Perform(IntAction act, int[] arr) { foreach (var item in arr) { //calling the delegate act(item); } }
static void Main(string[] args) { IntAction act = PrintInt; act += Console.WriteLine; int[] tab = { 1, 2, 3, 4, 5 }; Perform(act, tab); }
static void Main(string[] args) { int[] array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; IntAction action = Console.WriteLine; // 1. 집계자 개체 사용 var aggregator = new Aggregator(); action += aggregator.Add; ForEach(array, action); Console.WriteLine($"Sum: {aggregator.Sum}"); // 2. delegate 사용 ////int sum = 0; ////action += delegate (int n) ////{ //// sum += n; ////}; ////ForEach(array, action); ////Console.WriteLine($"Sum: {sum}"); // 3. Lambda 사용 ////int sum = 0; ////// 3.1 장황한 ////action += (int n) => ////{ //// sum += n; ////}; ////// 3.2 매개변수 형식 생략 ////////action += (n) => ////////{ //////// sum += n; ////////}; ////// 3.3 하나의 매개변수에 대해 괄호 생략 ////////action += n => ////////{ //////// sum += n; ////////}; ////// 3.4 하나의 구문에 대해 '{}' 생략 ////////action += n => sum += n; ////ForEach(array, action); ////Console.WriteLine($"Sum: {sum}"); }
static void Main(string[] args) { //Question A IntAction act = PrintInt; act(42); //Qustion B int[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Perform(act, list); //Another way // Perform(act, 11, 12, 13, 14, 15, 17, 18, 20, 22, 12); }
static void Main(string[] args) { int[] array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; IntAction action = Console.WriteLine; int sum = 0; action += n => sum += n; ForEach(array, action); Console.WriteLine($"Sum: {sum}"); }
static void Main(string[] args) { //create an istance of the delegate IntAction intAction = PrintInt; intAction(42); Console.WriteLine("Print array elements:"); int[] arr = { 1, 3, 6, 2 }; Perform(PrintInt, arr); Console.WriteLine("Print variable number of parameters: "); PerformDifferently(PrintInt, 4, 6, 7, 0, 9, 23); Console.ReadLine(); }
public void Act(IntAction f) { ForEach(i => f(i)); }
void Start() { IntAction anyád = IntAction.CreateInstance <IntAction>(); likeStatChanged.Register(OnEventRaised); }