c# 委托
2021/4/18 12:55:08
本文主要是介绍c# 委托,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
namespace ConsoleApp2 { class Program { static void Main(string[] args) { Calculator calculator = new Calculator(); // 委托方式一(方法是没有返回值的) //Report方法后面() Action action = new Action(calculator.Report); // 直接调用 calculator.Report(); // 间接调用 action.Invoke(); // 简写 action(); // 委托方式二(方法带参数) // 第一个int是返回值,第二第三是方法参数 Func<int, int, int> func1 = new Func<int, int, int>(calculator.Add); Func<int, int, int> func2 = new Func<int, int, int>(calculator.Sub); int x = 100; int y = 200; int z = 0; // 方式1 z = func1(x, y); Console.WriteLine(z); // 方式2 z = func2.Invoke(x, y); Console.WriteLine(z); } } class Calculator { public void Report() { Console.WriteLine("I have 3 methods"); } public int Add(int a, int b) { int result = a + b; return result; } public int Sub(int a, int b) { int result = a - b; return result; } } }
自定义委托
namespace ConsoleApp2 { // 声明委托,类型约束,返回值是double, 有两个double形参 public delegate double Calc(double x, double y); class Program { static void Main(string[] args) { Calculator calculator = new Calculator(); Calc calc1 = new Calc(calculator.Add); Calc calc2 = new Calc(calculator.Sub); Calc calc3 = new Calc(calculator.Mul); Calc calc4 = new Calc(calculator.Div); double a = 100; double b = 200; //double c = calc1.Invoke(a, b); Console.WriteLine(calc1(a,b)); Console.WriteLine(calc2(a,b)); Console.WriteLine(calc3(a,b)); Console.WriteLine(calc4(a,b)); } } class Calculator { public void Report() { Console.WriteLine("I have 3 methods"); } public double Add(double a, double b) { double result = a + b; return result; } public double Sub(double a, double b) { double result = a - b; return result; } public double Mul(double a, double b) { double result = a * b; return result; } public double Div(double a, double b) { double result = a / b; return result; } } }
委托的一般使用
实例: 把方法当作参数传递给另一个方法
模板方法
namespace ConsoleApp2 { class Program { static void Main(string[] args) { ProductFactory productFactory = new ProductFactory(); WrapFactory wrapFactory = new WrapFactory(); // 返回值是Product Func<Product> fun1 = new Func<Product>(productFactory.MakePizza); Func<Product> fun2 = new Func<Product>(productFactory.MakeToyCar); Box box1 = wrapFactory.WrapProduct(fun1); Console.WriteLine(box1.Product.Name); Box box2 = wrapFactory.WrapProduct(fun2); Console.WriteLine(box2.Product.Name); } } class Product { public string Name { get; set; } } class Box { public Product Product { get; set; } } class WrapFactory { public Box WrapProduct(Func<Product> getProduct) { Box box = new Box(); Product product = getProduct.Invoke(); box.Product = product; return box; } } class ProductFactory { public Product MakePizza() { Product product = new Product(); product.Name = "Pizza"; return product; } public Product MakeToyCar() { Product product = new Product(); product.Name = "Toy Car"; return product; } } }
回调方法
namespace ConsoleApp2 { class Program { static void Main(string[] args) { ProductFactory productFactory = new ProductFactory(); WrapFactory wrapFactory = new WrapFactory(); // 返回值是Product Func<Product> fun1 = new Func<Product>(productFactory.MakePizza); Func<Product> fun2 = new Func<Product>(productFactory.MakeToyCar); Logger logger = new Logger(); // 参数是Product // 封装只有一个参数且不返回值的方法。 Action<Product> action1 = new Action<Product>(logger.Log); Box box1 = wrapFactory.WrapProduct(fun1, action1); Console.WriteLine(box1.Product.Name); Box box2 = wrapFactory.WrapProduct(fun2, action1); Console.WriteLine(box2.Product.Name); } } class Product { public string Name { get; set; } public double Price { get; set; } } class Logger { public void Log(Product product) { Console.WriteLine($"Product '{product.Name}' areate at {DateTime.UtcNow}. Price is {product.Price}"); } } class Box { public Product Product { get; set; } } class WrapFactory { public Box WrapProduct(Func<Product> getProduct, Action<Product> logCallback) { Box box = new Box(); Product product = getProduct.Invoke(); if (product.Price >= 50) { logCallback(product); } box.Product = product; return box; } } class ProductFactory { public Product MakePizza() { Product product = new Product(); product.Name = "Pizza"; product.Price = 12; return product; } public Product MakeToyCar() { Product product = new Product(); product.Name = "Toy Car"; product.Price = 100; return product; } } }
这篇关于c# 委托的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具