private void Btn_DelegateExamples_Click(object sender, EventArgs e) { //Instantiate delegate with named Methods DoOperation op = NamedVoidMethod; DoOperationWithReturn opReturn = NamedMethodWithStringReturn; //Invoke Delegate methods op("Required Par"); var returnedValueFromDelegate = opReturn("Required Par"); Console.WriteLine(returnedValueFromDelegate); //Instantiate with Anonymus method op = delegate(string par) { Console.WriteLine(par); }; op("3-You wrote this in anonymus method oh yeahh!!!!"); // Finally ..... Instantiate with lambda Instrucction // hey hey it's diferent to Lambda expression opReturn = par => { Console.WriteLine("4-Call Lambda Instruction!!!!"); return("Returned from delegate Method with lambda Instruction }:) "); }; var returnedValueFromDelegateLambdaInst = opReturn("Execute Lambda String Method!!!!"); Console.WriteLine(returnedValueFromDelegateLambdaInst); }
public static void Process(DoOperation operation, double x, double y) { var res = operation(x, y); Console.Write($" = {res}"); Console.WriteLine(); }
public void Do <O>(CanDoOperation <O> canDoOperation, DoOperation <O> doOperation) where O : class { foreach (var candidate in Values <O>().Where(candidate => canDoOperation(candidate))) { doOperation(candidate); return; } throw new ApplicationException(string.Format("No default for {0}", typeof(T).Name)); }
static void Main(string[] args) { DelegateTest delegatetest = new DelegateTest(); delegatetest.DoStuff(); DoOperation someOperation = new DoOperation(MyMultiply); someOperation += MySum; }
private List <int> ExecuteOperation(Stats other, DoOperation opFunc) { List <int> statsThatChanged = new List <int>(); int[] statChanges = new int[] { other.MaxHP, other.Atk, other.Def, other.Map, other.Mar, other.Spd, other.Tec, other.Luk, other.Acc, other.Crt, other.Eva, other.Cev }; for (int i = 0; i < statChanges.Length; i++) { if (statChanges[i] == 0) { continue; } StatsList[i] = opFunc(StatsList[i], statChanges[i]); statsThatChanged.Add(i); } return(statsThatChanged); }
static void Main(string[] args) { Process(Plus, 10, 5); Process(Minus, 7, 5); DoOperation multiOperation = delegate(double x, double y) { Console.Write($"{x} * {y}"); return(x * y); }; Console.WriteLine($" = {multiOperation(5,5)}"); Console.WriteLine(); Console.ReadLine(); }
private void Btn_LambdaExample_Click(object sender, EventArgs e) { const string msg = "Lambda Example "; DoOperation lambdaVoid = par => Console.WriteLine(par); DoOperationWithReturn lambdaReturnStringInstruction = par => { return(par); }; DoOperationWithReturn lambdaReturnStringExpression = par => par; DoOperationWithReturnAndTwoParameters lambdaReturnStringInstructionTwoParameters = (par1, par2) => { return(par1 + par2); }; lambdaVoid(msg + 1); Console.WriteLine(lambdaReturnStringInstruction(msg + 2)); Console.WriteLine(lambdaReturnStringExpression(msg + 3)); Console.WriteLine(lambdaReturnStringInstructionTwoParameters(msg, 4.ToString())); }
public void Do <O>(CanDoOperation <O> canDoOperation, DoOperation <O> doOperation) where O : class { for (int priority = operators.Count - 1; priority >= 0; priority--) { for (int i = operators[priority].Count - 1; i >= 0; i--) { Operator <T, P> anOperator = operators[priority][i]; anOperator.Processor = Processor; var candidate = anOperator as O; if (candidate == null) { continue; } if (!canDoOperation(candidate)) { continue; } doOperation(candidate); return; } } throw new ApplicationException(string.Format("No default for {0}", typeof(T).Name)); }
/// <summary> /// Do an API operation where one account is operating on a targetted account. /// Takes all the request parameters and an operation to perfrom on the target account. /// The target account is specified by an accountID as the first parameter in the /// request URL. /// </summary> /// <remarks> /// The requesting account must be the target account or an administration account. /// </remarks> /// <param name="pReq"></param> /// <param name="pArgs"></param> /// <param name="pDoOp"></param> /// <returns></returns> public RESTReplyData APITargetedAccountOperation(RESTRequestData pReq, List <string> pArgs, DoOperation pDoOp) { RESTReplyData replyData = new RESTReplyData(); // The HTTP response info ResponseBody respBody = new ResponseBody(); if (Accounts.Instance.TryGetAccountWithAuthToken(pReq.AuthToken, out AccountEntity aAccount)) { string otherAcct = pArgs.Count > 0 ? pArgs[0] : null; if (otherAcct != null) { if (Accounts.Instance.TryGetAccountWithID(otherAcct, out AccountEntity targetAccount)) { // either the requestor is admin or the same account if (aAccount.Administrator || aAccount.AccountID == targetAccount.AccountID) { pDoOp(respBody, aAccount, targetAccount); } else { respBody.RespondFailure("Not account or administrator"); replyData.Status = (int)HttpStatusCode.Unauthorized; }; } else { respBody.RespondFailure("No such account"); replyData.Status = (int)HttpStatusCode.Unauthorized; }; } else { respBody.RespondFailure("Account not included in request URL"); } } else { respBody.RespondFailure("Unauthorized"); } replyData.SetBody(respBody, pReq); return(replyData); }
public OutputNeuron(DoOperation op) { operation = op; }
static void Main(string[] args) { /* Bank myBank = new Bank("BankName", 1000); * myBank.Add(500);*/ DoOperation myDelegate = new DoOperation(MyMultiply); myDelegate(4, 5); myDelegate += MySum; myDelegate(7, 8); /* * LinkedList<String> linked = new LinkedList<String>(); * linked.AddFirst("One"); * linked.AddLast("Ten"); * * LinkedListNode<String> newNode = linked.First; * linked.AddAfter(newNode, "Two"); * * LinkedListNode<String> newNode1 = linked.Last; * linked.AddBefore(newNode1, "Nine"); * * foreach(string value in linked) * { * Console.WriteLine(value); * } * * Console.WriteLine("***********************"); * * linked.Remove(newNode1); * * foreach (string value in linked) * { * Console.WriteLine(value); * } * * linked.RemoveFirst(); * * Console.WriteLine("***********************"); * * * foreach (string value in linked) * { * Console.WriteLine(value); * } * * linked.RemoveLast(); * * Console.WriteLine("***********************"); * * * foreach (string value in linked) * { * Console.WriteLine(value); * } * * linked.Clear(); * foreach (string value in linked) * { * Console.WriteLine(value); * }*/ /* Dictionary<int, string> week = new Dictionary<int, string>() * { * {1, "Monday" }, * {2,"Tuesday" }, * {3, "Wensday" }, * {4,"Thursday" }, * {5,"Friday" }, * {6, "Sutarday" }, * {7,"Sunday" } * }; * * Dictionary<int, string> arr = new Dictionary<int, string>(); * arr.Add(0, "one"); * arr.Add(5, "two"); * * Console.WriteLine(week[5]); * Console.WriteLine(week.Count); * * week.Remove(6); * foreach (KeyValuePair<int,string> keyValuePair in week) * { * Console.WriteLine(keyValuePair.Key + " - " +keyValuePair.Value); * } * * Console.WriteLine("******************************"); * * week.Add(6, "Sestadienis"); * foreach(KeyValuePair<int,string> keyValuePair1 in week) * { * Console.WriteLine(keyValuePair1.Key + " - " + keyValuePair1.Value); * } * * * if (week.Contains(new KeyValuePair<int,string>(5,"Friday"))) * { * Console.WriteLine("Yra"); * } * else * { * Console.WriteLine("Nera"); * } * * * if(week.ContainsKey(7)) * { * Console.WriteLine("Toks raktas yra"); * } * * * if(week.ContainsValue("Monday")) * { * Console.WriteLine("Tokia reiksme yra"); * } * * foreach(KeyValuePair<int,string> keyValuePair2 in arr) * { * Console.WriteLine(keyValuePair2.Key + " " + keyValuePair2.Value); * } * * arr.Clear();*/ /* File.WriteAllText(@"..\test2.txt", "Hello"); * * string failoTekstas = File.ReadAllText(@"..\test2.txt"); * Console.WriteLine(failoTekstas); * * File.AppendAllText(@"..\test2.txt", "C#"); * * * File.WriteAllText(@"..\test1.txt", "New file"); * File.Copy(@"..\test1.txt", @"..\test3.txt"); * * File.Move(@"..\test3.txt", @"..\test4.txt"); * * if (File.Exists(@"..\test1.txt")) * { * File.Delete(@"..\test1.txt"); * }*/ /*MyLibrary.Enemy testEnemy1 = new MyLibrary.Enemy("Enemy"); * * MyUtils.Enemy testEnemy2 = new MyUtils.Enemy(); * * testEnemy2.Name = "Enemy2"; * * * * testEnemy1.PrintName(); * testEnemy2.DoStuff();*/ /* * string wordsLine; * * wordsLine = "String object is a sequential collection of System.Char objects which represent a string."; * * char test = wordsLine[5]; * Console.WriteLine($"wordsLine[5]: {test}"); * int totalChars = wordsLine.Length; * Console.WriteLine($"wordsLine.Length: {totalChars}"); * * string testStr = wordsLine.Substring(7, 6); * Console.WriteLine($"wordsLine.Substring(7, 6): {testStr}"); * * string testStr1 = wordsLine.Remove(25); * Console.WriteLine($"wordsLine.Remove(25): {testStr1}"); * * string testStr2 = wordsLine.Replace('o', '@'); * Console.WriteLine($"wordsLine.Replace('o', '@'): {testStr2}"); * * string testStr3 = wordsLine.Insert(7, "zxzxzxzxz"); * Console.WriteLine($"wordsLine.Insert(7, zxzxzxzxz): {testStr3}"); * * string testStr4 = "Atttsts"; * string testStr5 = "BBBsskdskd"; * string testStr6 = String.Concat(testStr4, " ", testStr5); * Console.WriteLine($"String.Concat(testStr4, testStr5): {testStr6}");*/ }
private void GenerateClick(object sender, RoutedEventArgs e) { DoOperation.Invoke(); }