Delegate, Action, Func and Expression

Delegate is a pointer to a method. Example below shows how to create and use simple delegate.

    /// <summary>
    /// Simple delegate example
    /// </summary>
    class Program
    {
        //declaration of a pointer to a function which takes two parameters and returns string
        delegate string SimpleCalculationDelegate(int a, int b); 

        static void Main(string[] args)
        {
            //assignment of the function to the delegate
            SimpleCalculationDelegate myCalculation = (a, b) =>
            {
                return (a * b).ToString("f2");
            }; 

            string result = myCalculation(2, 3); // execution
            Console.WriteLine(result); // prints out 6
            Console.ReadLine();
        }
    }

You don’t really need to create your own SimpleCalculationDelegate. You can use existing Func.

    /// <summary>
    /// Simple delegate example - using Func
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //assignment of the function to the delegate
            Func<int, int, string> myCalculation = (a, b) =>
            {
                return (a * b).ToString("f2");
            }; 

            string result = myCalculation(2, 3); // execution
            Console.WriteLine(result); // prints out 6
            Console.ReadLine();
        }
    }

When your method doesn’t return any value, you can use Action instead of Func.

    /// <summary>
    /// Simple delegate example - using Func
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //assignment of the function to the delegate
            Action<int, int> myCalculation = (a, b) =>
            {
                Console.WriteLine((a * b).ToString("f2"));
            };

            myCalculation(2, 3); // execution
            Console.ReadLine();
        }
    }
	

You can use Func in LINQ queries.
Example for LINQ to objects.

    /// <summary>
    /// Func usage in LINQ queries
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            var allNumbers = new int[] { 1, 101, 2, 102 };

            //creates a method to filter out numbers
            Func<int, bool> bigNumbersFunc = (a) =>
            {
                return a > 100;
            };

            //filters out numbers
            var bigNumbers = allNumbers.Where(bigNumbersFunc);

            foreach (var n in bigNumbers)
                Console.WriteLine(n);

            Console.ReadLine();
        }
    }
	

Example for LINQ to SQL.

    /// <summary>
    /// Expression usage in LINQ queries
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {

            //creates a method to filter out numbers; Expression have better performance than Func
            Expression<Func<int, bool>> bigNumbersExpr = (a) => a > 100 && a <111;

            //filters out numbers
            using (var context = new TSQL2012Entities())
            {
                var bigNumbers = context.Nums.Select(x => x.n).Where(bigNumbersExpr);

                foreach (var n in bigNumbers)
                    Console.WriteLine(n);
            }
            Console.ReadLine();
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s