определить делегат 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