What is Func<T,TResult> in C#?

In simple terms,Func<T,TResult> is just generic delegate. Depending on the requirement,the type parameters(T and TResult) can be replaced with the corresponding(对应的) type arguments.

For example,Func<Employee,string> is a delegate that represents(代表) a function expecting(期待) Employee object as an input parameter and returns a string.

class Program
    {
        static void Main(string[] args)
        {
            List<Employee> employees = new List<Employee>() { 
                new Employee{ID=101,Name="lin1"},
                new Employee{ID=102,Name="lin2"},
                new Employee{ID=103,Name="lin3"}
            };

            //Func<Employee, string> selector = e => "name=" + e.Name;
            IEnumerable<string> employeeNames = employees.Select(e => "name=" + e.Name);
            foreach (string name in employeeNames)
            {
                Console.WriteLine(name);
            }

        }
    }
    class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
View Code

相关文章:

  • 2021-09-27
  • 2019-09-14
  • 2021-06-24
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2021-06-30
相关资源
相似解决方案