All with Contains using case insensitve

Pangram
Pangram in C#
using System;
using System.Linq;

public static class Pangram
{
    private static readonly StringComparison xcase = StringComparison.CurrentCultureIgnoreCase;

    public static bool IsPangram(string input)
    {
        return "abcdefghijklmnopqrstuvwxyz".All(c => input.Contains(c, xcase));
    }
}
  • This begins by setting a variable for a case-insensitive string comparison.
  • It then checks if all letters in the alphabet are contained in the input, using the LINQ method All with the String method Contains.
  • Contains takes the optional StringComparison argument, but a case-insensitive comparison is about seven times slower than if the input were lowercased and then an exact comparison were done.

Shortening

When the body of a function is a single expression, the function can be implemented as an expression-bodied member, like so

public static bool IsPangram(string input) =>
    "abcdefghijklmnopqrstuvwxyz".All(c => input.Contains(c, xcase));

or

public static bool IsPangram(string input) => "abcdefghijklmnopqrstuvwxyz".All(c => input.Contains(c, xcase));
8th May 2024 · Found it useful?