title: How to get number of elements in an array, Extension Method Count() and Length gives the size of Array.
body: <p>Hi I was trying to find the number of elements in an array </p>
  
  <pre><code>byte[] salt = new byte[32];
  </code></pre>
  
  <p>now I only have mentioned size 32 so the <strong>Length</strong> Property of Array and Enumerable's <strong>Count</strong> Method will give me <strong>32</strong>.</p>
  
  <p>Even if I will iterate on this Array <strong>salt</strong> 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)</p>
  
  <p>Now suppose I do:</p>
  
  <pre><code>for (int i = 0; i &lt; 5 ; i++)
          {
              salt[i] = 4-i;
          }
  </code></pre>
  
  <p>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. *<em>My question  Is there any other inbuilt function that could give me this number 5 ? *</em> 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.</p>