Advertisement
Caminhoneiro

//Lambda + Action + Func

Jun 21st, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. //Lambda + Action + Func
  2.  
  3.   public class ProcessData
  4.     {
  5.         public void Process(int x, int y, BizRulesDelegate del)
  6.         {
  7.             var result = del(x, y);
  8.             Console.WriteLine(result);
  9.         }
  10.  
  11.         public void ProcessAction(int x, int y, Action<int, int> action)
  12.         {
  13.             action(x, y);
  14.             Console.WriteLine("Action has been processed");
  15.         }
  16.  
  17.         public void ProcessFunc(int x, int y, Func<int, int, int> del)
  18.         {
  19.             var result = del(x, y);
  20.             Console.WriteLine(result);
  21.         }
  22.  
  23.     }
  24.  
  25.  
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Text;
  30. using System.Threading.Tasks;
  31.  
  32. namespace LambdasAndDelegates
  33. {
  34.     public delegate int BizRulesDelegate(int x, int y);
  35.  
  36.     class Program
  37.     {
  38.         static void Main(string[] args)
  39.         {
  40.             var custs = new List<Customer>
  41.             {
  42.                 new Customer { City = "Phoenix", FirstName = "John", LastName = "Doe", ID = 1},
  43.                 new Customer { City = "Phoenix", FirstName = "Jane", LastName = "Doe", ID = 500},
  44.                 new Customer { City = "Seattle", FirstName = "Suki", LastName = "Pizzoro", ID = 3},
  45.                 new Customer { City = "New York City", FirstName = "Michelle", LastName = "Smith", ID = 4},
  46.             };
  47.  
  48.             var phxCusts = custs
  49.                 .Where(c => c.City == "Phoenix" && c.ID < 500)
  50.                 .OrderBy(c => c.FirstName);
  51.  
  52.             foreach (var cust in phxCusts)
  53.             {
  54.                 Console.WriteLine(cust.FirstName);
  55.             }
  56.  
  57.             var data = new ProcessData();
  58.             BizRulesDelegate addDel = (x, y) => x + y;
  59.             BizRulesDelegate multiplyDel = (x, y) => x * y;
  60.             data.Process(2, 3, multiplyDel);
  61.  
  62.             Action<int, int> myAction = (x, y) => Console.WriteLine(x + y);
  63.             Action<int, int> myMultiplyAction = (x, y) => Console.WriteLine(x * y);
  64.             data.ProcessAction(2, 3, myMultiplyAction);
  65.  
  66.             Func<int, int, int> funcAddDel = (x, y) => x + y;
  67.             Func<int, int, int> funcMultipleDel = (x, y) => x * y;
  68.             data.ProcessFunc(3, 2, funcMultipleDel);
  69.  
  70.             var worker = new Worker();
  71.             worker.WorkPerformed += (s, e) =>
  72.             {
  73.                 Console.WriteLine("Worked: " + e.Hours + " hour(s) doing: " + e.WorkType);
  74.                 Console.WriteLine("Some other value");
  75.             };
  76.             worker.WorkCompleted += (s, e) => Console.WriteLine("Work is complete!");
  77.             worker.DoWork(8, WorkType.GenerateReports);
  78.  
  79.             Console.Read();
  80.         }
  81.  
  82.         //static void Worker_WorkPerformed(object sender, WorkPerformedEventArgs e)
  83.         //{
  84.         //    Console.WriteLine("Worked: " + e.Hours + " hours(s) doing: " + e.WorkType);
  85.         //}
  86.  
  87.         //static void Worker_WorkCompleted(object sender, EventArgs e)
  88.         //{
  89.         //    Console.WriteLine("Work is complete!");
  90.         //}
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement