Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 100 - Func delegate in c#

Suggested Videos 
Part 97 - Performance of a multithreaded program
Part 98 - Anonymous methods in c#
Part 99 - Lambda expression in c#



In this video we will discuss, the purpose of Func<T, TResult> delegate in c# with an example.

What is Func<T, TResult> in C#?
In simple terms, Func<T, TResult> is just a 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.

Program used in the demo:
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    public static void Main()
    {
        List<Employee> listEmployees = new List<Employee>()
        {
            new Employee{ ID = 101, Name = "Mark"},
            new Employee{ ID = 102, Name = "John"},
            new Employee{ ID = 103, Name = "Mary"},
        };

        // Create a Func delegate
        Func<Employee, string> selector =
            employee => "Name = " + employee.Name;
        // Pass the delegate to the Select() LINQ function
        IEnumerable<string> names = listEmployees.Select(selector);

        // The above output can be achieved using
        // lambda expression as shown below
        // IEnumerable<string> names =
        // listEmployees.Select(employee => "Name = " + employee.Name);

        foreach (string name in names)
        {
            Console.WriteLine(name);
        }
    }

    public 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.

using System;
class Program
{
    public static void Main()
    {
        Func<int, int, string> funcDelegate = (firstNumber, secondNumber) =>
            "Sum = " + (firstNumber + secondNumber).ToString();

        string result = funcDelegate(10, 20);
        Console.WriteLine(result);
    }
}

3 comments:

  1. thank you very much sir....these tutorials are very helpful....please upload tutorials on WINDOWS FORMS concepts like LINQ and CRYSTO REPORTS if possible....thank u again....

    ReplyDelete
  2. Hi,

    Thank you so much for such easy to understand and well explained tutorials! I have looked everywhere on the internet including some premium websites that offer tutorials and none of them have such a concise, to the point list. Thank you so much for these!

    ReplyDelete
  3. Hello there,
    I want to thank you so so much for the contents you have been uploading there are so so well-packaged and rich. They contain rich information that most online school websites can't sit to do.
    Thank You so much... I've been able to cover all I couldn't in 5 years in about 2 weeks.
    Again Thank you so much.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.