ExtensionMethod.NET Home of 828 C#, Visual Basic, F# and Javascript extension methods


Simple extension method Check if all items are the same in a List. This extension method will return true if all of the elements in the list are the same. The original author is MSkuta This extension method is getting from :https://stackoverflow.com/a/35839942/5483868


/// <summary>
///   Checks whether all items in the enumerable are same (Uses <see cref="object.Equals(object)" /> to check for equality)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerable">The enumerable.</param>
/// <returns>
///   Returns true if there is 0 or 1 item in the enumerable or if all items in the enumerable are same (equal to
///   each other) otherwise false.
/// </returns>
public static bool AreAllSame<T>(this IEnumerable<T> enumerable)
    if (enumerable == null) throw new ArgumentNullException(nameof(enumerable));

    using (var enumerator = enumerable.GetEnumerator())
        var toCompare = default(T);
        if (enumerator.MoveNext())
            toCompare = enumerator.Current;

        while (enumerator.MoveNext())
            if (toCompare != null && !toCompare.Equals(enumerator.Current))
                return false;

    return true;


if (items.AreAllSame()) ...

Author: Janson

Submitted on: 25 nov 2019

Language: csharp

Type: System.Collections.Generic

Views: 991