Know the size of each element of a list

1
Assuming I have a List<byte[]> Imagens; and I want to know if any element in this list has more than 1 mega, I could do this via foreach , checking item by item:

        foreach (var item in Imagens)
            if (item.Length > 1000000)
                throw new ArgumentException(msn);

What would be the way to do this?

The list must be invalid if any of its elements is larger than 1MB.

    
asked by anonymous 15.09.2018 / 17:09

1 answer

4

Only use Any .

if (imagens.Any(x => x.Length > 1_000_000)) //aí escolhe o que fazer

But do not throw an exception, this is inappropriate. Flow control can not be done with a mechanism of exceptional situations or programming errors (which should not be captured because nothing can be done in this case). If the question had more context I would give a more complete and appropriate solution.

I used 1,000,000, but this is not the same as 1MB which would be 1,048,576.

    
15.09.2018 / 17:47