Blogger news

Blogger templates

Friday, February 4, 2011

Func, Action, Delegate

Func<>,Action<>,Predicate<>

Now am starting with Func<>,Action<>,Predicate<>. These are  C# general purpose generic delegate. I will explain one by one.

Func<>
Func is a generic delegate, with Func<> we can point to any method which returns something. We cannot point to method with return type “void” with Func<>. I will explain it with example

Func<int, string> myFunc

This is a generic delegate which accept one parameter of type “int” and return “string” type (last one in the Func should be the return type) so we can point any method which accepts one parameter of type “int” and returning “string” with “Func”. So I can point the method below to this Func.
public static string GetNameandAge(int age)
        {
            return "Kiran "+" " + age.ToString();
        }
I can point this method to Func as,

Func<int, string> myFunc = GetNameandAge;

The whole program looks like,

class Program
    {
        static void Main(string[] args)
        {
            Func<int, string> myFunc = GetNameandAge;
            Console.WriteLine(myFunc(30));
            Console.ReadKey();
        }
        public static string GetNameandAge(int age)
        {
            return "Kiran "+" " + age.ToString();
        }
   
    }

Next I am going this to Lambda,

  Func<int, string> myFunc = age=>"Kiran "+" "+ age.ToString();
   Console.WriteLine(myFunc(30));
  Console.ReadKey();

Now also the output would be,
Kiran 30.

 
The whole program looks like,
class Program
    {
        static void Main(string[] args)
        {
            Func<int, string> myFunc = a=>"Kiran "+" "+a.ToString();
            Console.WriteLine(myFunc(30));
            Console.ReadKey();
        }

I will explain each part in this lambda,

Here “age” in left side of lambda is the same as the parameter in the previous method. Compiler automatically identify the the type of “age” as “int” and the right side of lambda is same as the return part of the previous method. Compiler is enough brilliant to understand all.


Another example,
Func<string, bool> myFunc

This is another  “Func”. The meaning of this “Func” is, with this I can point to any method which accepts a “string” parameter and returns “bool”. So I can point a method like this to this “Func”
public static bool MethodReturnBool(string name)
        {
            return name == "Kiran";
        }

The whole program looks like,
static void Main(string[] args)
        {
            Func<string, bool> myFunc = MethodReturnBool;
            Console.WriteLine(myFunc("Kiran"));
            Console.ReadKey();
        }

        public static bool MethodReturnBool(string name)
        {
            return name == "Kiran";
    }
Output is  “True”

Now lambda for the same,

Func<string, bool> myFunc = name => name == "Kiran";

Here “name” is the input parameter and  name == "Kiran" which returns bool value.

In C# 4.0 Func have 17 overloads. See the picture below.


“Func” must return something but can have input parameter or not.



Action<>

Action is another generic general purpose delegate. With “Action<>” we can point to any method which returns nothing means method with return void and input parameters as per delegate signature. For example I can point below method with “Action<>” delegate.,

public static void PrintSum(int a, int b)
        {
            Console.WriteLine(a + b);
        }
Whole program looks like,

using System;
namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Action<int, int> sum = PrintSum;
            sum (100, 150);
            Console.ReadKey();
        }

        public static void PrintSum(int a, int b)
        {
            Console.WriteLine(a + b);
        }
    }
}

In the example see Action<int,int> which means with this delegate I can point any method with return type void and have 2 input parameters of type int.

Output would be “250”.

Now am going to write the same  with lambda expression.


Action<int, int> sum = (a, b) => Console.WriteLine(a + b);

Here “a” and “b” are input parameters of type “int” just like the method we wrote before. Right side of lambda is the action need to execute just like Console.WriteLine we saw in the “PrintSum” method. Here right side of lambda is an action because Action<> delegate will not return anything. The whole program looks like,

using System;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Action<int, int> sum = (a, b) => Console.WriteLine(a + b);
            sum(100, 150);
            Console.ReadKey();
        }
    }
}
Now also the output would be “250”



Predicate<>

Predicate is another generic general purpose delegate. With we can point to any method which have one input parameter and returns bool. So I can point the method,

public static bool PointByPredicate(int num)
        {
            return num == 100;
        }
With predicate. Whole program looks like,

using System;

namespace PredicateExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Predicate<int> myPredicate = PointByPredicate;
            Console.WriteLine(myPredicate(100));
            Console.ReadKey();
        }

        public static bool PointByPredicate(int num)
        {
            return num == 100;
        }
    }
}

Output would be “True”.

Now am writing the same in “Lambda”,

Predicate<int> myPredicate = num => num == 100;

Here “num” is the input parameter to the lambda just as the input parameter in “PointByPredicate” method above.
Right side of lambda (num==100) is checking for num value. If num is hundred then return “True” else return “False”. This is same as return num == 100; in “PointByPredicate” method above. The whole program looks like,


using System;

namespace PredicateExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Predicate<int> myPredicate = num => num == 100;
            Console.WriteLine(myPredicate(100));
            Console.ReadKey();
        }
    }
}

Yes you are right output is “True” again.
Predicate<>

Predicate is another generic general purpose delegate. With we can point to any method which have one input parameter and returns bool. So I can point the method,

public static bool PointByPredicate(int num)
        {
            return num == 100;
        }
With predicate. Whole program looks like,

using System;

namespace PredicateExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Predicate<int> myPredicate = PointByPredicate;
            Console.WriteLine(myPredicate(100));
            Console.ReadKey();
        }

        public static bool PointByPredicate(int num)
        {
            return num == 100;
        }
    }
}

Output would be “True”.

Now am writing the same in “Lambda”,

Predicate<int> myPredicate = num => num == 100;

Here “num” is the input parameter to the lambda just as the input parameter in “PointByPredicate” method above.
Right side of lambda (num==100) is checking for num value. If num is hundred then return “True” else return “False”. This is same as return num == 100; in “PointByPredicate” method above. The whole program looks like,


using System;

namespace PredicateExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Predicate<int> myPredicate = num => num == 100;
            Console.WriteLine(myPredicate(100));
            Console.ReadKey();
        }
    }
}

Yes you are right output is “True” again.

No comments:

Post a Comment