static void Main(string[] args) { DoSomethingHandler myAction = DisplayMessage; int test = 0; myAction("Hello World"); myAction = delegate(string userText) { Console.WriteLine("Bruder: " + userText); test++; }; myAction("Hi " + test); myAction("Bye " + test); myAction = (string userText) => { Console.WriteLine("Schwester"); }; myAction(""); myAction = x => Console.WriteLine("Ja geil"); myAction(""); }
static void Main(string[] args) { DoSomethingHandler myAction = DisplayMessage; myAction("blub"); DoesMethodWork(myAction); }
static void Main(string[] args) { DoSomethingHandler <string> myAction = DisplayMessage; myAction("Hello Welt"); Action <string> myNewAction = DisplayMessage; //anomyme Methode (c# 2.0) //esto es un metodo anónimo no tiene nombre solo asignture //se puede usar solo en el main no desde otro lado myAction = delegate(string userText) { Console.WriteLine("Ausgabe anoyme Methode:"); Console.WriteLine(userText); }; myAction("Invoke anoyme Methode"); //esta es la forma de invocar este metodo desde aqui solamente //anomyme Methode (c# 3.0) myAction = (string userText) => { Console.WriteLine("Ausgabe vereinfachte anonyme Methode:"); Console.WriteLine(userText); }; myAction = userText => { Console.WriteLine("Ausgabe vereinfachte anonyme Methode:"); Console.WriteLine(userText); }; myAction = x => Console.WriteLine(x); myAction("Test"); int[] zahlenReihe = new int[] { 5, 21, 8, 9, 22, 50, 1, 96 }; // zahlenReihe = zahlenReihe.Where(CheckSizeRToFive).ToArray(); zahlenReihe = zahlenReihe.Where(x => x > 10).ToArray(); zahlenReihe = zahlenReihe.Select(x => x * x).ToArray(); List <int> sortedElements = new List <int>(); foreach (var item in zahlenReihe) { if (item > 10) { sortedElements.Add(item); } } zahlenReihe = sortedElements.ToArray(); //Func<int, bool> myFuntion = CheckSize; //int ist parameter bool recovert }
public void TestDelegate() { DoSomethingHandler del1 = new DoSomethingHandler(DoSomethingForReal); DoSomethingHandler del2 = new DoSomethingHandler(DoSomethingForPretend); del1 += del2; del1(2, "fish"); Assert.AreEqual("Something real done 2 times with fish\nSomething made up was done 2 times with fish\n", m_delMessage); }
static bool DoesMethodWork(DoSomethingHandler displayMethod) { bool result = false; try { displayMethod("Test"); result = true; } catch (Exception) { result = false; } return(result); }