Expanding Arrays

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

All

Can someone tell me why the following is considered bad coding practice,
please?

int n = 0;
int m = 1;

ArrayList list = new ArrayList();
list.Add(n);
list.Add(m);

Many thanks in advance!
 
Erm.. it isn't. That is perfectly fine if "list" represents a changable set
of data, that could later have more or less entries. The only down side is
that it isn't strongly typed, but in 2.0 that can be fixed using List<int>.
Maybe if you know the size in advance you could pre-size it in the ctor...
but there are more important things in life...

Of course, if it will only ever have 2 entries, then int[] may suffice...
but then again even then an array may be overkill...

What makes you think it is bad?
 
Chris said:
Can someone tell me why the following is considered bad coding practice,
please?

int n = 0;
int m = 1;

ArrayList list = new ArrayList();
list.Add(n);
list.Add(m);

It's not, particularly.

In .NET 2.0 it would be better to use a List<int> to avoid boxing and
unboxing (and casting) but there's nothing hugely wrong with the above.

Perhaps you could give us more context for your question?

Jon
 
All

Thanks for the replies, and apologies for the misleading subject line -
that's what multitasking on a Friday afternoon does for you.
Can someone tell me why the following is considered bad coding practice,
please?

int n = 0;
int m = 1;

ArrayList list = new ArrayList();
list.Add(n);
list.Add(m);

It's not, particularly.
[..]

Perhaps you could give us more context for your question?

The question I asked was in an interview test for prospective .NET
developers that I was attempting. I am fairly new to .NET and C#, so I was
wondering if there was something that I was missing. Then only thing that I
could see was that with a fixed length collection such as this, one could
use a strongly typed Array ionstead of the ArrayList. From your responses it
seems that this is the correct answer.

Thanks!
 
The code is quite good... Nothing much wrong there.
it can be improved as per the above suggestions...

one more better point that we can include is fixing the side of list if
you can guess it approximately other wise it is quite good code.

Thanks
-Cnu.
 
Back
Top