TGViewer
C# (C Sharp) programming C# (C Sharp) programming @csharp_ci · 18.1K subscribers
Post #755 4.8K
🖥 Необходимо определить делегат и реализовать метод

⏩Итак, вот задание:
определить делегат bool CounterHashSetDelegate(int a) и реализовать метод int Function12(HashSet<int> intSet, CounterHashSetDelegate filter), который возвращает количество элементов из intSet, которые удовлетворяют условию filter.
public static  class 
{
public delegate bool CounterHashSetDelegate(int a);
public static int Function12(HashSet<int> intSet, CounterHashSetDelegate filter)
{

int count = 0;
foreach (int i in intSet)
{
if(filter(i))
count++;
}
return count;
}
}



⏩Давайте сразу к сути, решение может выглядеть так:
public delegate bool CounterHashSetDelegate(int a);

class Program
{
static void Main(string[] args)
{
CounterHashSetDelegate d = IsEvenNum;

Console.WriteLine($"Количество четных элементов:{Function12(new HashSet<int> { 1, 2, 3, 4, 5 }, d)}");

d = IsGreaterThen;

Console.WriteLine($"Количество элементов > 2:{Function12(new HashSet<int> { 1, 2, 3, 4, 5 }, d)}");

Console.ReadKey();
}

public static bool IsEvenNum(int a)
{
return a % 2 == 0;// четное ли число например
}

public static bool IsGreaterThen(int a)
{
return a > 2;//больше ли например
}


public static int Function12(HashSet<int> intSet, CounterHashSetDelegate filter)
{

int count = 0;
foreach (int i in intSet)
{
if (filter(i))
{
count++;
}
}
return count;
}

}

Вот и все дела

@csharp_ci
More from @csharp_ci
  1. Oct 13, 2026🧱 Модульный монолит легко потерять одним удобным JOIN. Допустим, модуль заказов напрямую…
  2. Oct 11, 2026Задачка по C# 14: обновили проект на .NET 10, и код молча начал работать иначе Что выведет…
  3. Oct 9, 2026Post #1901
  4. Oct 7, 2026⚡️ Microsoft переработала C# Dev Kit: меньше памяти, быстрее запуск В версии 11.0 шесть пр…
  5. Oct 6, 2026🛠 Вышел Microsoft.Data.SqlClient 7.1.1 Обновление .NET-драйвера для SQL Server исправляет…
  6. Oct 5, 2026Одна задача, три языка: Rust, C# или C++? В курсе Google *Comprehensive Rust* есть упражне…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →