C# अभ्यास

Custom Enumerable Extension

Extension methods can add domain vocabulary while retaining static type safety.

How it works

Create a reusable extension method for sequences.

The generic constraint is unnecessary here because the operation only needs IEnumerable<T>.

Runnable C# example

C#
Run code →
Program.cs
using System;
using System.Collections.Generic;
static class EnumerableExtensions
{
    public static bool None<T>(this IEnumerable<T> source, Func<T, bool> predicate)
    {
        foreach (T item in source) if (predicate(item)) return false;
        return true;
    }
}
class Program
{
    static void Main() => Console.WriteLine(new[] { 1, 3, 5 }.None(number => number % 2 == 0));
}
Expected output
True

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.