yo chauhan
yo chauhan

Reputation: 12295

How to get number of elements in an array, Extension Method Count() and Length gives the size of Array.

Hi I was trying to find the number of elements in an array

byte[] salt = new byte[32];

now I only have mentioned size 32 so the Length Property of Array and Enumerable's Count Method will give me 32.

Even if I will iterate on this Array salt using for or foreach or any other looping construct it will iterate 32 times and on each index the value is 0 (i.e default value of byte)

Now suppose I do:

for (int i = 0; i < 5 ; i++)
        {
            salt[i] = 4-i;
        }

And I want to know how many elements are inserted sequentially in Array starting from index 0, Here you may say are you fool you iterating it 5 times and you know the number is 5 , but I am having heavy looping and other logic (appending prepending to another arrays of byte) in it. *My question Is there any other inbuilt function that could give me this number 5 ? * Even if I iterate and check for first default value and break the loop there and get the count but there might be the chance last value inserted is 0 like above salt[4] is 0 so that iterating will give me the count 4 which is incorrect . If I am not wrong I think when we declare Array with size like 32 above 32 consecutive memory bytes are reserved and you are free to insert at any index from 0-31 and its your responsibility to keep the track where and how and how many elements are assigned to Array .I hope you got my point And thanks in advance for help.

Upvotes: 0

Views: 1237

Answers (3)

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

An array is an array, and in .NET is initialized when it is allocated. Once it's initialized, the question of whether a given value is uninitialized or simply 0 isn't something that's possible to check. A 0 is a 0.

However, you can bypass that in several ways. You can use a List<int>, like @SLaks suggested, to have a dynamically allocated list that's only initialized with the elements you want.

You can also use, instead of an array of int, and array of int?, so a null value isn't the same as a 0.

Upvotes: 4

Zlatomir
Zlatomir

Reputation: 7034

Short answer is you can't, the array contains 32 integers, .net framework doesn't care if some of them are 0, so you can create your own function that counts how many integers from an array are different than 0, or keep a "count" when you assign values for array elements or something like that.

Or you can use another container, example a list and dynamically add or remove integers from it.

Upvotes: 3

Immortal Blue
Immortal Blue

Reputation: 1700

Ok, when you define an array as int[] myArray = int[32]; you are saying, I HAVE 32 ints. Not, create me space for 32 ints that I will fill in later. That's why count is giving you 32.

If you want something which you can genuinly add to and resize, you need to use a List (or one of it's relatives.

If you want to have a "cap" for a list, I found this :Maximum capacity collection in c#

Upvotes: 2

Related Questions