译文链接:https://www.infoworld.com/art...

委托是一个类型平安的函数指针,它能够援用与委托具备雷同签名的办法。委托罕用于实现回调办法或者事件机制,在C#中个别用 "delegate" 关键字申明。你能够申明一个和类平级的委托,也能够嵌套在类中。

Func 和 Action 是什么,如何应用?

两者最根本的区别是,前者适宜那些须要带返回值的委托,后者适宜那些不带返回值的委托。

Func 所援用的办法接管一个或者多个入参并带有一个返回值,Action所援用的办法接管一个或者多个参数并且没有返回值,换句话说,你的委托所援用的办法没有返回值,这时候适宜用 Action。

Predicate所援用的办法接管一个或者多个泛型参数并且返回一个 bool 值,你能够假设它等价于 Func<T,bool>,Predicate 罕用于对 collection 进行一组条件检索。

C# 中应用 Action

你能够应用 委托 去实现事件和回调办法,C#委托十分相似于C++中的函数指针,然而 C# 中的 委托 是类型平安的,你能够将办法作为参数传递给委托从而让委托指向该办法。

上面的代码片段展现了 Action 委托的语法结构。

Action<TParameter>

接下来的代码清单展现了如何应用 Action 委托,当上面的代码执行完结后会在控制台打印 Hello !!!

        static void Main(string[] args)        {            Action<string> action = new Action<string>(Display);            action("Hello!!!");            Console.Read();        }                static void Display(string message)        {            Console.WriteLine(message);        }

C# 中应用 Func

当初咱们一起学习下 Func 委托,上面是 Func 的语法结构。

Func<TParameter, TOutput>

接下来的代码片段展现了如何在 C# 中应用 Func 委托,最终办法会打印出 Hra(根本薪资的 40%) 的值,根本薪资是作为参数传下去的,如下代码所示:

        static void Main(string[] args)        {            Func<int, double> func = new Func<int, double>(CalculateHra);            Console.WriteLine(func(50000));            Console.Read();        }        static double CalculateHra(int basic)        {            return (double)(basic * .4);        }

值得注意的是,Func 委托的第二个参数示意办法的返回值,在下面这个例子中,它就是计算后的 Hra 值,作为 double 型返回。

C# 中应用 Predicate

Predicate 委托罕用于检索 collection,上面是 Predicate 的语法结构。

Predicate<T>

值得注意的是, Predicate<T> 差不多等价于 Func<T,bool>

思考上面的 Customer 实体类。

    class Customer    {        public int Id { get; set; }        public string FirstName { get; set; }        public string LastName { get; set; }        public string Address { get; set; }        public string City { get; set; }        public string State { get; set; }        public string Country { get; set; }    }

接下来生成一个 customer 汇合并且丢一些数据进去,如下代码:

            List<Customer> custList = new List<Customer>();            custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });            custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });

接下来是残缺的代码片段展现了如何应用 Predicate 去检索汇合。

        static void Main(string[] args)        {            List<Customer> custList = new List<Customer>();            custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });            custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });            Predicate<Customer> hydCustomers = x => x.Id == 1;            Customer customer = custList.Find(hydCustomers);            Console.WriteLine(customer.FirstName);            Console.Read();        }

当下面的代码被胜利执行, 控制台将会输入 Joydip

更多高质量干货:参见我的 GitHub: dotnetfly