static void Main(string[] args) { SimonVdMeer svdm = new SimonVdMeer(); WillemKlein wk = new WillemKlein(); //wk.Reken(svdm.Add, 3,4); //wk.Reken(svdm.Subtract, 8, 7); // Multicast delegate MathDel m1 = svdm.Add; m1 += svdm.Subtract; m1 += Multiply; m1 += svdm.Subtract; foreach (var met in m1.GetInvocationList()) { Console.WriteLine(met.Method.Name); } int res = m1(1, 2); Console.WriteLine(res); }
public void Reken(MathDel berekening, int a, int b) { Console.WriteLine("Willem Klein rekent nu..."); int result = berekening(a, b); // TODO: Rekenen Console.WriteLine($"Het resultaat is: {result}"); }
public void Calculate(MathDel bliep, int a, int b) { Console.WriteLine("Willem gaat rekenen..."); var result = bliep.Invoke(a, b); Console.WriteLine(result); }
static void Main(string[] args) { int res = Add(1, 2); // .NET 1.0/1.1 (2002/2003) MathDel m1 = new MathDel(Add); res = m1(2, 3); // .NET 2.0 MathDel m2 = Add; res = m2(3, 4); int c = 100; MathDel m3 = delegate(int a, int b) { return(a + b + c); }; res = m3(4, 5); // .NET 3.0 (2007) // Lambda's MathDel m4 = (a, b) => a + b + c; res = m4(5, 6); // Voorgedefinieerde delegates // EventHandler (oeroude) // Predicate<int> (in onbruik geraakt) // Procedures. Action // Functions. Func<int> Action <string> cw = Console.WriteLine; // cw(res.ToString()); Func <int, int, int> m5 = Add; res = m5(6, 7); Func <int, int, int, int> m6 = (int a, int b, int c) => a + b + c; res = m6(1, 2, 3); res = Subtract(4, 5); // C# 7.3 (2017) int Subtract(int a, int b) { return(a - b + c); }; Console.WriteLine(res); }
static void Main(string[] args) { // Framework 1.0/1.1 (2002/2003) MathDel m1 = new MathDel(Add); int result = m1(1, 2); // Framework 2.0 (2005) MathDel m2 = Add; result = m2(2, 3); int c = 10; MathDel m3 = delegate(int a, int b) { return(a + b); }; result = m3(3, 4); // Framework 3.0 (2007) // Lambda expressie MathDel m4 = (a, b) => a + b; result = m4(4, 5); // Functions Func <int, int, int> m5 = Add; result = m5(5, 6); Func <int, int, int> m6 = (a, b) => a + b; result = m6(6, 7); // Procedures Action <int> cw = nr => Console.WriteLine(nr); cw(result); // C# 8.0 int InlineAdd(int a, int b) { return(a + b); } result = InlineAdd(7, 8); cw(result); //Console.WriteLine(result); }
static void Main(string[] args) { int c = 100; // Framework 1.0/1.1 (2002, 2003) MathDel m1 = new MathDel(Add); int res = m1(1, 2); // Framework 2.0 (2005) MathDel m2 = Add; res = m2(2, 3); // Nieuw!!!!! Inline functies MathDel m3 = delegate(int a, int b) { return(a + b); }; res = m3(3, 4); // Framework 3.0 (2008) // Lambda expressions MathDel m4 = (a, b) => a + b; res = m4(4, 5); // Procedures Action <string> a1 = Console.WriteLine; //a1(res.ToString()); // Functions Func <int, int, int> m5 = Add; res = m5(5, 6); Func <int, int, int> m6 = (a, b) => a - b; res = m6(7, 8); // 2018 (C# 8.0) int InlineAdd(int a, int b) { return(a + b); } res = InlineAdd(6, 7); Console.WriteLine(res); }
static void Main(string[] args) { // .NET 1.0/1.1 MathDel m1 = new MathDel(Add); int result = m1(1, 2); // .NET 2.0 MathDel m2 = Add; result = m2(2, 3); int c = 10; MathDel m3 = delegate(int a, int b) { return(a + b); }; result = m3(3, 4); // .NET 3.0 and beyond MathDel m4 = (a, b) => a + b; result = m4(4, 5); // Procedures // Can be used for procedurers with up to 16 arguments Action <string> m5 = (s) => Console.WriteLine("Hi " + s); m5("Patrick"); // Functions // For function with up to 16 arguments // The last one betweer the brackets always represents the return type Func <int, int, int> m6 = Add; result = m6(7, 8); // Predicates // For functions that return a boolean // Func<string bool> does the same Predicate <string> p1 = s => s == "hi"; Console.WriteLine(p1("hi")); Console.WriteLine(result); }
static void Main(string[] args) { // Framework 1.0/1.1 MathDel m1 = new MathDel(Add); var result = m1(1, 2); // Framework 2.0 MathDel m2 = Add; result = m2(2, 3); int c = 10; MathDel m3 = delegate(int a, int b) { return(a + b + c); }; result = m3(3, 4); // Framework 3.0/3.5 MathDel m4 = (a, b) => a + b; result = m4(5, 6); // Functions: Func<int> Func <int, int, int> m5 = (a, b) => a + b; result = m5(7, 8); // Procedures: Action<> Action <int> cw = Console.WriteLine; cw(result); //Console.WriteLine(result); // C# 8 int LocalAdd(int a, int b) { return(a + b); } result = LocalAdd(9, 10); cw(result); }
static void Main(string[] args) { SimonvdMeer svdm = new SimonvdMeer(); WillemKlein wk = new WillemKlein(); //wk.Calculate(svdm.Subtract, 8, 4); MathDel m1 = svdm.Add; m1 += svdm.Add; m1 += svdm.Subtract; m1 -= svdm.Subtract; m1 += svdm.Subtract; foreach (var mi in m1.GetInvocationList()) { Console.WriteLine(mi.Method.Name); } var res = m1(1, 2); Console.WriteLine(res); }
static void Main(string[] args) { //2 Object and collection initialization Product pr = new Product { MfgDate = new DateTime(2015, 10, 4), Name = "kenneth", ProductID = 1 }; List <Product> prods = new List <Product> { pr, new Product { MfgDate = new DateTime(2015, 10, 4), Name = "kenneth1", ProductID = 2 }, new Product { MfgDate = new DateTime(2015, 10, 4), Name = "kenneth2", ProductID = 3 }, new Product { MfgDate = new DateTime(2015, 10, 4), Name = "kenneth3", ProductID = 4 } }; //3 Implicitly typed local variables var i = 10; var str = "hello"; var fl = 5645.67576; //4 Anonymous Data Types var student = new { StudentID = 10, Name = "student1", DOB = new DateTime(2015, 10, 10) }; //5 Delegates -->> Anonymous methods (2.0) -->> lamda //5a Anonymous method--> Inline function MathDel del = delegate(int n1, int n2) { return(n1 + n2); }; Action <Product> prdaction = delegate(Product p) { Console.WriteLine(p.Name); }; prods.ForEach(prdaction); prods.ForEach(delegate(Product p) { Console.WriteLine(p.Name); }); //5b lamda MathDel del2 = (n1, n2) => { return(n1 + n2); }; prods.ForEach((p) => { Console.WriteLine(p.Name); }); MathDel del3 = (n1, n2) => n1 + n2; prods.ForEach((p) => Console.WriteLine(p.Name)); //6a Utility/Extension Methods string msg = "Tesco"; Console.WriteLine(msg.Welcome()); //7 Dynamic Data Type dynamic pr1 = new Product { MfgDate = new DateTime(2015, 10, 4), Name = "kenneth4", ProductID = 5 }; //pr1.Display(); //Display() method does not exist--> error oly at runtime //8 Func delegate Func <int, int, long> funcmul = (n1, n2) => n1 * n2; Console.WriteLine(funcmul.Invoke(22, 33)); Console.ReadLine(); }