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. 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
  2. I am looking for and I love to post a comment that "The content of your post is awesome" Great work! Wordpress Tips

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

    ReplyDelete
  6. 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
  7. 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
  8. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

    IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

    corporate training in chennai corporate training in chennai

    corporate training companies in india corporate training companies in india

    corporate training companies in chennai corporate training companies in chennai

    I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai

    ReplyDelete
  9. logo design, Very nice article. I enjoyed reading your post. very nice share. I want to twit this to my followers. Thanks !. If you need then visit us! and thanks again for the post.

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