Array with no size limit

3
   ClsPessoa[] Pessoa = new ClsPessoa[10];

I'm new to C # and would like some help. How can I declare an array of a class as in the example, without inserting a size limit.

    
asked by anonymous 11.10.2015 / 20:48

3 answers

7

The array has fixed size, defined, can not change anymore. You have to use another type. The most appropriate is a List<T> where T is the type of the list. With it you can add elements on demand. Take a good look at the documentation and ask specific questions here.

In fact, most of the time you should prefer a list. array should only be used when there is a good reason for it.

As a curiosity, the array is used internally within the type List as a concrete implementation. But it manages the necessary manipulations when the size is not enough. There is no magic, only abstraction.

In your example it would look like this:

var pessoas = new List<ClsPessoa>();

or if you want to reserve 10 positions:

var pessoas = new List<ClsPessoa>(10);

This should interest you .

And please do not use ArrayList as suggested in another answer.

    
11.10.2015 / 20:53
3

Use the class List<T>

List<ClsPessoa> pessoas = new List<ClsPessoa>();
    
11.10.2015 / 20:53
1
ArrayList Pessoa = new ArrayList();

Note: The System.Array class should be used.

Source: link

    
11.10.2015 / 20:53