Created
February 29, 2024 17:14
-
-
Save GigaOrts/de6d1e4a517e1c9e0071b6a6fcdcc463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using System.Threading.Channels; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<int> nums = new(); | |
Generate(nums); | |
var nums2 = nums.Where(IsMoreThanFive); | |
var nums3 = nums.Where(num => num > 5); | |
var nums4 = Where(nums, num => num > 5); | |
var nums5 = Where(nums, IsMoreThanFive); | |
Print(nums2); | |
Print(nums3); | |
Print(nums4); | |
Print(nums5); | |
} | |
private static bool IsMoreThanFive(int number) | |
{ | |
return number > 5; | |
} | |
private static IEnumerable<int> Where(IEnumerable<int> list, Predicate<int> predicate) | |
{ | |
var newList = new List<int>(); | |
foreach (var item in list) | |
{ | |
if(predicate(item)) | |
{ | |
newList.Add(item); | |
} | |
} | |
return newList; | |
} | |
private static void Print(IEnumerable<int> numbers) | |
{ | |
Console.WriteLine("---------------------"); | |
foreach (var item in numbers) | |
{ | |
Console.WriteLine(item); | |
} | |
} | |
private static void Generate(List<int> numbers) | |
{ | |
for (int i = 0; i < 10; i++) | |
{ | |
numbers.Add(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment