简体   繁体   中英

C#: How to check if all the elements in an array of boolean variables are of the same value (All true or All false)?

I have an array of boolean variables and I want to return true if all the elements in this array are of the same value, false otherwise. I know I can loop through all the elements, but I'm wondering if there are any faster ways in C#.

var allAreTheSame = myArray.All(a => a) || myArray.All(a => !a)
var result = array.Distinct().Count() == 1;
// Assuming the array is NOT empty
// Get first value
var firstValue = myArray.First();

// Check if all other values are identical
var allidentical = myArray.Skip(1).All(z => z == firstValue);
Enumerable.Range(0, array.Length-1).All(i => array[i] == array[i+1])

编辑:注释后修复

Enumerable.Range(1, array.Length).All(i => array[i] == array[0])
var allAreTheSame = myArray.Distinct().Count() == 1

这只是David方法的一种替代方法,因为我认为枚举器组合将导致Array仅循环一次,所以它的长度略短并且效率更高。

I would go for an extension method. I'd always love those methods:

The class containing the extension method will be:

public static class ExtensionMethods
{
    public static bool AreAll<T>(this T[] source, Func<T, bool> condition)
    { return source.Where(condition).Count() == source.Count(); }

    public static bool AreAllTheSame<T>(this IEnumerable<T> source)
    { return source.Distinct().Count() == 1; }
}

You see that I have 2 extension methods, one taking a Func and one taking no parameter.

The first one is called when you want to check if all the elements in the array has the same value (for example, all elements are true, or all elements are false).

The second one is called, when you don't want to check against a specific parameter, but if you just want to see if all the values are the same.

And than a little demo to demonstrate the extension method itself:

class Program
{
    static void Main(string[] args)
    {
        bool[] array = { true, false, true, false, true };
        bool[] trueArray = { true, true, true, true };

        Console.WriteLine("Searching with a predicate:");
        Console.WriteLine(array.AreAll(x => x).ToString());
        Console.WriteLine(array.AreAll(x => !x).ToString());
        Console.WriteLine(trueArray.AreAll(x => x).ToString());
        Console.WriteLine(trueArray.AreAll(x => !x).ToString());

        Console.WriteLine("Searching without a predicate:");
        Console.WriteLine(array.AreAllTheSame().ToString());
        Console.WriteLine(array.AreAllTheSame().ToString());
        Console.WriteLine(trueArray.AreAllTheSame().ToString());
        Console.WriteLine(trueArray.AreAllTheSame().ToString());

        Console.ReadLine();
    }
}

This will produce the following output:

在此处输入图片说明 Let's hope it helps.

Just for fun a little different solution:

!(array.Any(b => b) && array.Any(b => !b));

There are two loops here. One of them should exit on the first element in the array. The other one should exit on the first occurrence that is different from the first in the array. It will also return true for empty arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM