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 !
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Delagate
- {
- delegate bool MeDelagate(int n);
- class Program
- {
- //static bool LessThanFive(int n) { return n < 5; }
- // static bool LessThanTen(int n) { return n < 10; }
- //static bool GreaterThanThirteen(int n) { return n > 13; }
- static void Main(string[] args)
- {
- int[] numbers = new[] { 2, 7, 3, 7, 9, 17,5, 1, 8, 1700 };
- IEnumerable<int> result = RunNumbersThroughGauntlet(numbers, n => n > 1500);
- foreach (int n in result)
- Console.WriteLine(n);
- }
- static IEnumerable<int> RunNumbersThroughGauntlet(IEnumerable<int> numbers, MeDelagate gauntlet)
- {
- foreach (int number in numbers)
- if (gauntlet(number))
- yield return number;
- }
- }
- }
3 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sreekanth ReddyPosted Jan 1, 2019, 11:21 PM
Rupal PatelPosted Jan 2, 2019, 5:40 AM
Jignesh KumarPosted Jan 1, 2019, 11:09 PM