Know the size of each element of a list

Asked

Viewed 452 times

1

Assuming I have one List<byte[]> Imagens; and I want to know if any element on that list has more than 1 mega, I could do it 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 that?

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

  • 2

    I should never make an exception to this. And I would need to explain the criterion better. Just 1 be out of this specification and should close the algorithm always? And the object is a array of bytes the amount of elements in essence is the same as the amount bytes. obter o tamanho total da lista em bytes, assim através de uma simples divisão saberia-se se a Lista contem elementos maiores que 1 mega. Is it? Why? No image, of course, is less than 1MB?

  • I will modify the question and let it more direct, but I will not expose here in what context this will be used because I believe that is not the focus, I believe that this would be more suitable for Codereview.

  • @Matheussaraiva provide the context of the question, can help to find a better solution to your problem.

1 answer

4


Only use the Any.

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

I put in the Github for future reference.

But don’t make an exception, this is inappropriate. Flow control cannot 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 adequate solution.

I used 1,000,000, but that’s not the same as 1MB that would be 1,048,576.

  • I didn’t know this syntax (1_000_000) was possible. What is the difference of it to 1000000? And why you say not the same as 1MB? This list will receive binarized images and by business rules can not have more than one mega. Each element in the list corresponds to an image.

  • About releasing the exception, a bit of context. This code is part of the validation of a Model property where only the verification is done. Treating the flow in this case is the responsibility of another layer and not the Model. Therefore the choice to launch the exception, so that those who go to work the other layer do the treatment and determine the necessary flow.

  • Assuming that each item in the list has exact 1Mb each, a list with 10 elements would have in theory 10Mb. So before I was looking for a way to get the size of the list and not of each element, so I could do something like if(imagens.ByteSize / imagens.Count != 1000000), obviously this example is a pseudo code.

  • 1

    It is possible since C# 7, it is only to be more readable. Each Kbyte has 1024 bytes, and 1MB has 1024 * 1024 bytes. Never use exception for validation. It is full of question on the subject here. Assumptions are not used to compute anything, only certainties.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.