如何使用 C# 中的 Action, Func,Predicate

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

委托是一个类型安全的函数指针,它能够引用与委托具备相同签名的方法。委托经常使用于实现回调方法或者事件机制,在C#中通常用 "delegate" 关键字声明。你能够声明一个和类平级的委托,也能够嵌套在类中。html

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

二者最基本的区别是,前者适合那些须要带返回值的委托,后者适合那些不带返回值的委托。git

Func 所引用的方法接收一个或者多个入参并带有一个返回值,Action所引用的方法接收一个或者多个参数而且没有返回值,换句话说,你的委托所引用的方法没有返回值,这时候适合用 Action。github

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 的语法结构。code

Func<TParameter, TOutput>

接下来的代码片断展现了如何在 C# 中使用 Func 委托,最终方法会打印出 Hra(基本薪资的 40%) 的值,基本薪资是做为参数传下去的,以下代码所示:htm

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
相关文章
相关标签/搜索