Junior — Middle
Použili ste niekedy filtre vo svojich projektoch?
sobes.tech AI
Odpoveď od AI
Áno, filtre sa často používajú v C# na spracovanie kolekcií dát, napríklad pomocou LINQ. Filter umožňuje vybrať z množiny iba tie prvky, ktoré spĺňajú určitú podmienku.
Príklad použitia filtra s LINQ:
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> {1, 2, 3, 4, 5, 6};
// Filtrujeme iba párne čísla
var evenNumbers = numbers.Where(n => n % 2 == 0);
foreach (var num in evenNumbers)
{
Console.WriteLine(num); // Vypíše 2, 4, 6
}
}
}
Filtre sú užitočné na zjednodušenie spracovania dát a zlepšenie čitateľnosti kódu.