Ronald Rex

Ronald Rex

  • NA
  • 69
  • 915

Delegates and Lambda Expressions

Dec 27 2018 7:49 PM

12/27/2018 7:47:30 PM 

Hello Friends,

I was wondering do I need to refactor this codes that is using a delegate with a lambda expression, or is this best practice? Thanks !
 
  1. namespace Delagate  
  2. {  
  3.     delegate bool MeDelagate(int n);  
  4.   
  5.     class Program  
  6.     {  
  7.         //static bool LessThanFive(int n) { return n < 5; }  
  8.        // static bool LessThanTen(int n) { return n < 10; }  
  9.         //static bool GreaterThanThirteen(int n) { return n > 13; }  
  10.   
  11.         static void Main(string[] args)  
  12.         {  
  13.             int[] numbers = new[] { 2, 7, 3, 7, 9, 17,5, 1, 8, 1700 };  
  14.             IEnumerable<int> result = RunNumbersThroughGauntlet(numbers, n => n > 1500);  
  15.             foreach (int n in result)  
  16.                 Console.WriteLine(n);  
  17.         }  
  18.         static IEnumerable<int> RunNumbersThroughGauntlet(IEnumerable<int> numbers, MeDelagate gauntlet)  
  19.         {  
  20.             foreach (int number in numbers)  
  21.                 if (gauntlet(number))  
  22.                     yield return number;  
  23.         }  
  24.     }  
  25. }  
 


Answers (2)