Is there any method to find the number of elements that are filled in an array

lets say i have the following array defined

string[] text = new string[50]

and i fill the first 8 elements/indexes

Is there any method that could return the number of elements/indexes within an string array that are filled?

I tried using text.lenght() but that returns the total size of array. In above case it would return 50 which is not what i want...

Dani AI

Generated

This thread already has the right direction — use a resizable collection when you need the “real” number of filled entries — but there are a few practical alternatives and pitfalls to be aware of depending on constraints.

As and suggested, prefer a generic collection (List<string>) when possible because it maintains a live Count and avoids manual bookkeeping. If a fixed-size array must be used (interop, memory layout, legacy API), choose one of these patterns:

  • Maintain a counter while inserting (fast, O(1) updates):

    int filled = 0;
    // when adding:
    text[filled++] = newValue;
  • Compute on demand with LINQ (works when values may be null/empty; O(n) scan):

    using System.Linq;
    int filled = text.Count(s => !string.IsNullOrWhiteSpace(s));
  • If entries are appended from index 0 with no holes, find the last non-empty slot (faster than counting all if array is large):

    int last = Array.FindLastIndex(text, s => !string.IsNullOrWhiteSpace(s));
    int filled = (last >= 0) ? last + 1 : 0;

Notes and gotchas: the “stop at first null” loop approach (similar to ) only works when the array is filled contiguously from index 0 — any gaps break it. The foreach/count attempt in ’s post also needs to avoid redeclaring the counter and should check for null/empty. If count updates are frequent, maintain a counter; if queries are rare, on-demand LINQ or FindLastIndex is simpler. Avoid ArrayList in modern code — use List<T> for type safety and better performance.

Recommended Answers

All 9 Replies

Hello, gallian99.
I haven't heard about such standard functions :) I can offer two alternative ways for you:
1. Using reference types (like ArrayList). You'll able to add elements dynamically and get the actual size of your array.
2. Create your own structure/class, that will contain your array, the full array size (in your case it's 50) and number of added elements.

Thanks Anteka,

I have tackled it using an alternate already. But I should better try ArrayList as well. Haven't work around with it yet :)

Hello, gallian99.
I haven't heard about such standard functions :) I can offer two alternative ways for you:
1. Using reference types (like ArrayList). You'll able to add elements dynamically and get the actual size of your array.
2. Create your own structure/class, that will contain your array, the full array size (in your case it's 50) and number of added elements.

If you are running on .NET 3, you can use Lists, which are very easy:

List<String> strings = new List<String>(); //initialise the list
strings.Add(str); //Add an item
int length = strings.Count; //how many strings are in the list.

hi
u can do this

int counter=0;  // global value 
while(true)
            {
               
                if (s[counter] != null)
                {
                    comboBox1.Items.Add(s[counter]);
                    counter++;
                }
                else
                {
                    break;
                }
            }

bye

You should follow scru's advice and use a List<T> if you want to have a resizable container.

An ArrayList is also resizable, still usable but a bit "out of date".
If I where you follow the advise off scru and Rashakil Fol.

Thanks!

try this ??? is easy way to find

int I = 0;
foreach (string S in Text)
{
   int I += 1;
}
Console.WriteLine(I.ToString());

try this ??? is easy way to find

Well this is about the dumbest reply I've read on this forum. Did you even read the question?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.