Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lambda + Action + Func
- public class ProcessData
- {
- public void Process(int x, int y, BizRulesDelegate del)
- {
- var result = del(x, y);
- Console.WriteLine(result);
- }
- public void ProcessAction(int x, int y, Action<int, int> action)
- {
- action(x, y);
- Console.WriteLine("Action has been processed");
- }
- public void ProcessFunc(int x, int y, Func<int, int, int> del)
- {
- var result = del(x, y);
- Console.WriteLine(result);
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LambdasAndDelegates
- {
- public delegate int BizRulesDelegate(int x, int y);
- class Program
- {
- static void Main(string[] args)
- {
- var custs = new List<Customer>
- {
- new Customer { City = "Phoenix", FirstName = "John", LastName = "Doe", ID = 1},
- new Customer { City = "Phoenix", FirstName = "Jane", LastName = "Doe", ID = 500},
- new Customer { City = "Seattle", FirstName = "Suki", LastName = "Pizzoro", ID = 3},
- new Customer { City = "New York City", FirstName = "Michelle", LastName = "Smith", ID = 4},
- };
- var phxCusts = custs
- .Where(c => c.City == "Phoenix" && c.ID < 500)
- .OrderBy(c => c.FirstName);
- foreach (var cust in phxCusts)
- {
- Console.WriteLine(cust.FirstName);
- }
- var data = new ProcessData();
- BizRulesDelegate addDel = (x, y) => x + y;
- BizRulesDelegate multiplyDel = (x, y) => x * y;
- data.Process(2, 3, multiplyDel);
- Action<int, int> myAction = (x, y) => Console.WriteLine(x + y);
- Action<int, int> myMultiplyAction = (x, y) => Console.WriteLine(x * y);
- data.ProcessAction(2, 3, myMultiplyAction);
- Func<int, int, int> funcAddDel = (x, y) => x + y;
- Func<int, int, int> funcMultipleDel = (x, y) => x * y;
- data.ProcessFunc(3, 2, funcMultipleDel);
- var worker = new Worker();
- worker.WorkPerformed += (s, e) =>
- {
- Console.WriteLine("Worked: " + e.Hours + " hour(s) doing: " + e.WorkType);
- Console.WriteLine("Some other value");
- };
- worker.WorkCompleted += (s, e) => Console.WriteLine("Work is complete!");
- worker.DoWork(8, WorkType.GenerateReports);
- Console.Read();
- }
- //static void Worker_WorkPerformed(object sender, WorkPerformedEventArgs e)
- //{
- // Console.WriteLine("Worked: " + e.Hours + " hours(s) doing: " + e.WorkType);
- //}
- //static void Worker_WorkCompleted(object sender, EventArgs e)
- //{
- // Console.WriteLine("Work is complete!");
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement