Func Delegates in Actual Projects

In this blog I shall demonstrate how to use the Func Delegate in .Net framework.

Before going through this article I recommend you get some knowledge of Delegates. There are many articles and blogs in c-sharpcorner.com.

Step 1: Create a class named Customer.

using System;

using System.Collections.Generic;
using
System.Linq;
using
System.Text;

namespace FuncDelegates

{
   
public class Customer

   
{

        public int ID { get; set; }

        public string Name { get; set; }

        public int Points { get; set; }

   
}

}

Step 2: Create a class named Utility as shown below.

using System;

using System.Collections.Generic;
using
System.Linq;
using
System.Text;

namespace FuncDelegates

{
  
public class Utility

   
{

      static List<Customer> CustomerList = null;

       /// <summary>

       ///

       /// </summary>

       /// <param name="lstCustomer"></param>

       /// <param name="custDelegate">

       /// This is a Func delegate which takes an input string and outputs a boolean value.

       /// </param>

       /// <returns></returns>

       public static List<Customer> GetEligibleCustomers(List<Customer> lstCustomer,Func<Customer,bool> custDelegate)

       {

           CustomerList = new List<Customer>();

           foreach (Customer cust in lstCustomer)

           {

               // for each customer the func delegate in invoked to check whether he/she satifies the condition passed from client.

               if (custDelegate(cust))

               {

                   CustomerList.Add(cust);

               }
           }

           return CustomerList;

       }

   
}

}

The function GetEligibleCustomers has got 2 parameters.

  1. List<Customer>
  2. Func<Customer,bool>

The second parameter is of Func delegate type. This delegate accepts Customer as input and returns a Boolean value.

In my previous article I have created a delegate and that delegate was used in code.

For reference please go through the article Delegates in Actual Projects.

In this example I have used the predefined Func delegate in .Net framework.

Step 3: Main Class

using System;

using System.Collections.Generic;
using
System.Linq;
using
System.Text;

namespace FuncDelegates

{
   
class Program

   
{

        static List<Customer> customerList=null;

        
static void Main(string[] args)
        {

            customerList = new List<Customer>();

            Customer cust1 = new Customer { ID = 1, Name = "Praveen", Points = 1000 };
            Customer cust2 = new Customer { ID = 2, Name = "Ajai", Points = 420 };

            Customer cust3 = new Customer { ID = 3, Name = "Sabari", Points = 200 };

            Customer cust4 = new Customer { ID = 2, Name = "Anju", Points = 660 };

            Customer cust5 = new Customer { ID = 2, Name = "Madhav", Points = 600 };

            customerList.Add(cust1);

            customerList.Add(cust2);

            customerList.Add(cust3);

            customerList.Add(cust4);

            customerList.Add(cust5);
 

            Console.WriteLine("The Customer DataBase....... ");

            Console.WriteLine("**********************************************************************");
            foreach (var customer in customerList)

            {

                Console.WriteLine("ID: {0}, Name : {1} , Points {2}", customer.ID, customer.Name, customer.Points);

            }

            Console.WriteLine("**********************************************************************");
            Console.WriteLine("Eligible Customers : Points >500");

            Console.WriteLine("**********************************************************************");

            var eligibleCustomers = Utility.GetEligibleCustomers(customerList, e => e.Points > 500);

            foreach (var cust in eligibleCustomers
            {

                Console.WriteLine("ID: {0}, Name : {1} , Points {2}", cust.ID, cust.Name, cust.Points);

            }

            Console.ReadLine();

        }

   
}

}

Please have a look at the highlighted code. The GetEligibleCustomers function accepts 2 parameters.

  • customerList (List of customers).
  • e => e.Points > 500 (The criteria function is passed. The Func delegates points to this function).

For each customer in the customer list, the Func delegate is invoked to check whether that customer is an eligible customer or not.

For a better understanding I have uploaded the source code.

Output