Rupal Patel

Rupal Patel

  • NA
  • 76
  • 1.6k

Delegates and Lambda Expressions

Jan 1 2019 9:23 PM
Hi Friends, I was just wondering in this code why was I able to pass the Delagate in as a parameter for the lambda expression. I thought that a delegate was used as a pointer to a method or function, and then pass the Delegate as a parameter. Thanks !
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Delagate  
  8. {  
  9.     delegate bool MeDelagate(int n);  
  10.   
  11.     class Program  
  12.     {  
  13.         //static bool LessThanFive(int n) { return n < 5; }  
  14.        // static bool LessThanTen(int n) { return n < 10; }  
  15.         //static bool GreaterThanThirteen(int n) { return n > 13; }  
  16.   
  17.         static void Main(string[] args)  
  18.         {  
  19.             int[] numbers = new[] { 2, 7, 3, 7, 9, 17,5, 1, 8, 1700 };  
  20.             IEnumerable<int> result = RunNumbersThroughGauntlet(numbers, n => n > 1500);  
  21.             foreach (int n in result)  
  22.                 Console.WriteLine(n);  
  23.         }  
  24.   
  25.           
  26.         static IEnumerable<int> RunNumbersThroughGauntlet(IEnumerable<int> numbers, MeDelagate gauntlet)  
  27.         {  
  28.             foreach (int number in numbers)  
  29.                 if (gauntlet(number))  
  30.                     yield return number;  
  31.         }  
  32.     }  
  33. }  

Answers (3)