Validating that all strings in an array match a condition I want to validate a list of objects for example strings. and if one of the objects fails to pass the condition return false as validation result . this is the code I use :

    public static bool AreValid(string[] strs)
    {
        foreach (string str in strs)
        {
            if (str != condition )
            {
                return false; // does this breaks the for loop ?
                // break;     // no need to this
            }
        }

        return true;
    }


is this a correct approach ?
Yes, return immediately* returns from the method, no matter where in the method you are. You don't need the break.

But there is even easier way to write this code, using the LINQ method All():

strs.All(str =&gt; str == condition)


This also returns as soon as single non-matching element is found and is more readable.



* Actually, finally blocks run before you actually return from the method, but that's not relevant here.