Part 100 Func delegate in c#

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; }
    }

What is the difference between Func delegate and lambda expression?

They‘re the same, just two different ways to write the same thing. The lambda syntax is newer, more concise(简洁) and easy to write.

What if I have to pass two or more input parameters?

As of this recording there are 17 overloaded versions of Func, which enables us to pass variable number and type of input parameters. In the example below, Func<int,int,string> represents a function that expects 2 int input parameters and returns a string.

class Program
    {
        static void Main(string[] args)
        {
            Func<int, int, string> funcDelegate = (num1, num2) => (num1 + num2).ToString();
            string result = funcDelegate(10,20);
            Console.WriteLine("sum="+result);

        }
    }

Finally, I completely learning the  C# language, go to bed, good night guy.

时间: 2024-10-17 22:06:53

Part 100 Func delegate in c#的相关文章

C# for Beginner Part 98 to 100

Part 98   Anonymous methods in c# What is an anonymous method? Anonymous method is a method without a name. Introduced in C# 2.0,they provide us a way of creating delegate instances without having to write a separate method. class Program { static vo

Expression&lt;Func&lt;TObject, bool&gt;&gt;与Func&lt;TObject, bool&gt;的区别

Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后就会变成delegate,才能运行.比如 Expression<Func<int, bool>> ex = x=>x < 100; Func<int, bool> func = ex.Compile(); 然后你就可以调用func: func(5) //-返回

[C#] 委托Action和Func

一.说明 一般我们定义委托都是有如下两步: public delegate void MyDelegate(string name);//定义委托public MyDelegate myDelegate; //使用委托 .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-col

委托之Action和Func区别

一.说明 一般我们定义委托都是有如下两步: public delegate void MyDelegate(string name);//定义委托 public MyDelegate myDelegate; //使用委托 但.Net也提供了定义好的委托,我们可以直接使用. 二.定义 System.Action 无返回值 Action: public delegate void Action (); Action< T >: public delegate void Action< T &

C#中常见的委托(Func委托、Action委托、Predicate委托)

今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predicate全面解析 首先来说明Func委托,通过MSDN我们可以了解到,Func委托有如下的5种类型: (1) *delegate TResult Func<TResult>(); (2)*delegate TResult Func<T1,TResult>(T1 arg1); (3) *delegate TResult Func&

lambda表达式Expression&lt;Func&lt;Person, bool&gt;&gt; 、Func&lt;Person, bool&gt;区别

前言: 自己通过lambda表达式的封装,将对应的表达式转成字符串的过程中,对lambda表达式有了新的认识 原因: 很多开发者对lambda表达式Expression<Func<Person, bool>> .Func<Person, bool>表示存在疑惑,现在就用代码举个简单列子 原代码: using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expres

浅谈C#中常见的委托&lt;Func,Action,Predicate&gt;(转)

http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html 一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不用多废话了. 今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predi

Lambda expressions , Action , Func and Predicate

http://www.tutorialsteacher.com/csharp/csharp-action-delegate lambda 表达式 Action,func lambda表达式是什么,其有什么优点,不使用lambda 其的目的:简化代码. 在JAVA当中一般是使用接口来实现 Action is also a delegate type defined in the System namespace. An Action type delegate is the same as Fun

关于delegate(代理)总结

stackoverflow  上讲解:http://stackoverflow.com/a/12660523/4563358 delegate是将需要处理交给自己的代理. 在自己的对应的类中.h文件中申明对应的delegate @class CSPopMenu; @protocol CSPopMenuDelegate <NSObject> @optional -(void)PopMenuDimiss:(CSPopMenu *)popMenu; @end 插入一个可选择的方法,定义一个协议. @