Array with no size limit

Asked

Viewed 8,198 times

3

   ClsPessoa[] Pessoa = new ClsPessoa[10];

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

  • List<ClsPessoa> Pessoa = new List<ClsPessoa>;, with the limit of 2,147,483,648 elements. It’s a lot of people!

3 answers

9


The array has fixed size, set can not change anymore. You have to use another type. The most suitable 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. The 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 concrete implementation. But it manages the necessary manipulations when 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 don’t use ArrayList as suggested in another reply. Also avoid this Cls is hungarian notation.

  • What would then be the correct syntax for me to call the Add_pessoa Method for example? example: people[0]. Add_person(Txt_name.Text, Bday.Displaydate, Txt_email.Text);

  • This is not part of your original question, so it would be unfair to my other answers to have something else you asked in the comment. So open another question by putting the details of your class ClsPessoa. I answer there. So it gets more organized too.

  • I was going to ask why not use arraylist but I saw that the tag was C# and not java kkk

  • 1

    @Diegofelipe I don’t know if you think this is the reason not to use it. C# has ArrayList but it is not generic and should be preferred to use the generic version. Even if it has no type, it is better to make a List<object>. There is gain. And I think the name was bad too. The original was copied from the Java that named unfortunately.

  • @bigown understood, in college have always advised to use the List<> same, as equivalent more suitable to java Arraylist. As a matter of fact, I didn’t even know there was a girl of the same name in C#.

4

Use the class List<T>

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

1

  • 2

    The class Array has only uses to manipulate the array. The class ArrayList is obsolete and should not be used.

  • @bigown, I saw in that comment that you say that Arraylist is obsolete. Recently I started using Arraylist and helped me a lot, in case the List<T> would be equivalent to Arraylist ?

  • @Diegofarias exactly. The ArrayList is problematic.

  • @bigown I have little knowledge of the subject, but I found it easier to manipulate data array within Arraylist, otherwise I would have to create List<List<T>> understand ?

  • @Diegofarias do what’s right, it’s much better.

  • @bigown ok, thank you.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.