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. This is a very amazing post for cheap web hosting services. in this post, you have provided all the basic information regarding.

    website builder for reseller

    ReplyDelete
  2. Comfortabl y, the post is really the freshest on that deserving topic. I harmonise with your conclusions and definitely will thirstily look forward to your next updates.

    private label website builder

    ReplyDelete
  3. Thank you for the link building list.I am going jot down this because it will help me a lot.Great blog! Please keep on posting such blog.

    white label website builder

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

    ReplyDelete
  5. This is a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it
    website designing Service

    ReplyDelete
  6. Flutter Programming is one of the most popular in the cross platforms industry. We build excellent flutter mobile app and also learn how to overcome performance issues. is the best choice for all startups to discuss and get their project quotation. Offshore Flutter Development Service We also enable many startups to do Proof of concept and help them to learn about business decisions.

    ReplyDelete
  7. 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
  8. I am looking for and I love to post a comment that "The content of your post is awesome" Great work! สมัครเน็ตทรู

    ReplyDelete
  9. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    website designing

    ReplyDelete
  10. 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

Post a Comment