Understanding Predicate delegate with simple example in C#

Predicate is a delegate that always returns boolean value (True or False).

Predicate is used in conjunction with lambda expression.

Below example shows how can we simply replace static method with Predicate.


public class Program
{
 public static void Main()
 {
  
  Predicate<int> isGreaterThan5Predicate = m => m > 5;
  Console.WriteLine("Using Predicate to determine condition - 4 is not greater than 5 hence: " + isGreaterThan5Predicate(4));
  
  Console.WriteLine("Using static method to determine condition - 6 is greater than 5 hence: " + isGreaterThan5(6));

 }
 
 public static bool isGreaterThan5(int number)
 {
  return number > 5? true: false;
 }
}

Running above code will produce those messages in the console.

Using Predicate to determine condition - 4 is not greater than 5 hence: False
Using Predicate to determine condition - 6 is greater than 5 hence: True


Comments

  1. I am looking for and I love to post a comment that "The content of your post is awesome" Great work! Wordpress Tips

    ReplyDelete
  2. Crossplatform app development is challenging nowadays. many tools like a flutter and react-native are doing work really awesome. App development in Austin in cross-platform also have challenges like performance and network issue.

    ReplyDelete
  3. I am looking for and I love to post a comment that "The content of your post is awesome" Great work! สมัครเน็ตทรู

    ReplyDelete
  4. Financial sector has undergone drastic technological shifts in a comparatively short span of time. The shifts have been incorporated because of Growing Technology, Customer Demands, etc. Take a glance over some of the legacy Cross-platform App Development Company. These are so dynamic and efficient that it will cost you less time, more functionality, good output returns.

    ReplyDelete
  5. It offers both powerful scripting and fast application development process across a vast range of fields. unindent does not match any outer indentation level python

    ReplyDelete
  6. This type of assignments strained the budget of most of the web enterprises but still they had perforce to go in for such web development orders since they desperately wanted websites at their disposal. bog5 in ua

    ReplyDelete
  7. We recently covered the importance of professional website design as well as how a website design company can help you benefit from a customized website. A professional website design company can assist you in leveraging the power of the Internet in a number of ways. Website Design Company Dubai

    ReplyDelete

Post a Comment