Jan 23, 2020

PREDICATE DELEGATE IN C# (GENERIC DELEGATE p-3)

c# predicate delegate, c# delegate, delegate,predicate delegate, example of predicate delegate, predicate delegate example,lambda expression in c#
predicate delegate in c# 

PREDICATE DELEGATE IN C#

            Predicate delegate is one of the generic delegate which is used when our method return the value type as bool means the method just only return true or false, it is used for the bool values returning methods. ‘Predicate’ is the keyword for the use of this delegate it is compulsory that the method must have the return value as bool.
            In this part there is no overload method only delegate that’s accepts one parameter that has to pass and about return type predefined it already know the return values or output type either it may be true or false. So no need to explain output type only parameter list along with predicate keyword. but in Func delegate it is require to tell the output type. and the action delegate which have no return type

Syntax of predicate delegate

Predicate <parameter list > Obj = <method name> 
In place of predicate we can use func delegate how can? By explained that the parameter passing data type and return always will be bool. Syntax will be
func <parameter list, bool > Obj = <method name>
but it is better to use the predicate instead of func. 
It can be short if we use lambda expression on Predicate delegate not only Predicate all the three generic delegate can make easy by using lambda expression how can we make it just remove the method name and specify the lambda expression  as we did in lambda expression part like this.
Action <formal parameter type only > Obj = (actual parameter list only name) =>
{
            Method implementation;
}
you can see what is formal parameter and actual parameter by clicking on them.
            Predicate delegate contain only one delegate no more overload that take one parameter and return one output that are either true or false only which is as in table
Sr.no
Delegate
Parameter list
Return value number
1
One delegate
1 parameter
One return type
True/false

EXAMPLE OF PREDICATE DELEGATE IN C#

            Perform a simple program if a string contains more than 8 word return true else false using two examples, 1st example do it by normal delegate and method and in 2nd example do the same program by using predicate delegate along with lambda expression.
1st example without using predicate
namespace GenericDelegate
{
    public delegate bool Checkthis(string str);
    class Program
    {
        public static bool Check(string str)
        {
            if (str.Length>8)
                return true;
            return false;
        }
        static void Main(string[] args)
        {
            Checkthis ck = new Checkthis(Check);
            bool bo =ck("hello12345");
            Console.WriteLine(bo);
            Console.ReadKey();
        }
    }
}

2nd example by using predicate delegate
namespace GenericDelegate
{
    class Program
    {
        static void Main(string[] args)
        {
            Predicate<string> ck = (str) =>
            {
                if (str.Length > 8)
                    return true;
                return false;
            };
            bool bo =ck("hello");
            Console.WriteLine(bo);
            Console.ReadKey();
        }
    }
}

0 comments:

Post a Comment

Please do not enter any spam link in the comment box