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.
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.
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);
And please do not use ArrayList
as suggested in another answer.
Use the class List<T>
List<ClsPessoa> pessoas = new List<ClsPessoa>();