using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Action action = new Action(Method); Console.WriteLine(action.GetType().IsClass); } static void Method() { } } }
Example1:c++
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Action action = new Action(Method); action.Invoke(); action(); } static void Method() { Console.WriteLine("Hello Delegate"); } } }
Example2:算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { static void Main(string[] args) { Action<string, int> action = new Action<string, int>(Method); action.Invoke("張三",18); action("李四",19); } static void Method(string name, int age) { Console.WriteLine($"我叫{name}今年{age}歲"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { static void Main(string[] args) { Func<double, double, double> func = new Func<double, double, double>(Add); double result1 = func.Invoke(1.5,3.5); Console.WriteLine(result1); double result2 = func(2.5,4.5); Console.WriteLine(result2); } static double Add(double x, double y) { double result = x + y; return result; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { static void Main(string[] args) { Func<string> func = new Func<string>(FindName); SayHello(func); } static void SayHello(Func<string> FindDelegate) { Console.WriteLine($"Hello {FindDelegate()}"); } static string FindName() { return "小明"; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp3 { public delegate Product ProductDelegate(); public delegate void LogDelegate(Product product); class Program { static void Main(string[] args) { WrapProduct product = new WrapProduct(); Logger logger = new Logger(); WrapFactory wrapFactory = new WrapFactory(); ProductDelegate productDelegate1 = new ProductDelegate(product.GetToy); ProductDelegate productDelegate2 = new ProductDelegate(product.GetStationery); LogDelegate logDelegate = new LogDelegate(logger.Log); Box box1 = wrapFactory.GetBox(productDelegate1,logDelegate); Box box2 = wrapFactory.GetBox(productDelegate2,logDelegate); Console.WriteLine($"Product1 {box1.Product.Name} the price is {box1.Product.Price}"); Console.WriteLine($"Product2 {box2.Product.Name} the price is {box2.Product.Price}"); } } public class Product { public string Name { get; set; } public int Price { get; set; } } public class Box { public Product Product { get; set; } } public class WrapFactory { public Box GetBox(ProductDelegate productDelegate,LogDelegate logDelegate) { Box box = new Box(); Product product = productDelegate.Invoke(); if (product.Price>50) { logDelegate.Invoke(product); } box.Product = product; return box; } } public class WrapProduct { public Product GetToy() { Product product = new Product(); product.Name = "Toy"; product.Price = 100; return product; } public Product GetStationery() { Product product = new Product(); product.Name = "Stationery"; product.Price = 30; return product; } } public class Logger { public void Log(Product product) { Console.WriteLine($"Product {product.Name} created at {DateTime.Now.ToString()}"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp4 { class Program { static void Main(string[] args) { Func<int, int, int> func1 = new Func<int, int, int>((int a, int b) => { return a + b; }); var func2 = new Func<int,int,int>((a, b) => { return a * b; }); Calculate(func1,2,3); Calculate(func2,3,4); Calculate((x,y)=> { return x - y; },7,1); } static void Calculate<T>(Func<T,T,T> func,T x,T y) { T z = func.Invoke(x,y); Console.WriteLine(z); } } }
namespace ConsoleApp5 { class Father { } class Son : Father { } class Program { static void Main(string[] args) { Func<Father> func = new Func<Father>(Method); } static Son Method() { Son son = new Son(); return son; } } }
namespace ConsoleApp5 { class Father { } class Son : Father { } class Program { static void Main(string[] args) { Action<Son> action = new Action<Son>(Method); } static void Method(Father father) { } } }
namespace ConsoleApp6 { class Program { static void Main(string[] args) { var func = new Func<int, int, int>(M1); func += M2; func += M3; func(3,3); Console.WriteLine(func(3,3)); } static int M1(int x, int y) { Console.WriteLine(x+y); return x + y; } static int M2(int x, int y) { Console.WriteLine(x - y); return x - y; } static int M3(int x, int y) { Console.WriteLine(x*y); return x * y; } } }
namespace ConsoleApp7 { class Program { static void Main(string[] args) { var action = new Action(M1); action += M2; action += M3; action.Invoke(); action -= M2; action.Invoke(); } static void M1() { Console.WriteLine("M1 is invoked"); } static void M2() { Console.WriteLine("M2 is invoked"); } static void M3() { Console.WriteLine("M3 is invoked"); } } }
namespace ConsoleApp8 { class Program { static void Main(string[] args) { var action1 = new Action(M1); var action2 = new Action(M2); var action3 = new Action(M3); Action action = null; action = (Action)Delegate.Combine(action,action1); action = (Action)Delegate.Combine(action,action2); action = (Action)Delegate.Combine(action,action3); action(); Console.WriteLine(); action = (Action)Delegate.Remove(action,action2); action(); } static void M1() { Console.WriteLine("M1 is invoked"); } static void M2() { Console.WriteLine("M2 is invoked"); } static void M3() { Console.WriteLine("M3 is invoked"); } } }
namespace ConsoleApp9 { class Program { static void Main(string[] args) { Thread thread1 = new Thread(new ThreadStart(M1)); Thread thread2 = new Thread(new ThreadStart(M2)); thread1.Start(); thread2.Start(); } static void M1() {} static void M2() {} } }
namespace ConsoleApp10 { class Program { static void Main(string[] args) { Task task = new Task(new Action(M1)); task.Start(); } static void M1() { } } }
namespace ConsoleApp11 { class Program { static void Main(string[] args) { Calculator calculator = new Calculator(); Func<int, int, int> func = new Func<int, int, int>(calculator.Add); IAsyncResult asyncResult = func.BeginInvoke(2,3,null,null); Console.WriteLine($"結果是{func.EndInvoke(asyncResult)}"); Console.WriteLine("計算完成"); } } public class Calculator { public int Add(int a, int b) { return a + b; } } }