Creating an array

  • Thread starter Thread starter Martin Racette
  • Start date Start date
M

Martin Racette

Hi,

I new to C#, and I would like to know how I can create an array that I do
not know in advance how many item will be placed there
 
Martin said:
I new to C#, and I would like to know how I can create an array that I
do not know in advance how many item will be placed there
Use ArrayList in .NET 1 or List<T> in .NET 2 (or any other more appropriate
collection). You can think of those as "growable arrays".
 
Maybe this will help?

class Program
{
static void Main(string[] args)
{
// Get an array of ints.
int[] i = GetSomeIntArray<int>(123);

// Get an array of objects.
object[] o = GetSomeIntArray<object>(456);
}

private static T[] GetSomeIntArray<T>(int size)
{
return new T[size];
}
}
 
Martin said:
Hi,

I new to C#, and I would like to know how I can create an array that
I do not know in advance how many item will be placed there

Say you have the number of items in numItems, and you want an array of
integers, then you can do:

int[] myArray = new int[numItems];
 
As I was saying I do not know how many items there will be in the array, so
I can not specify the size of it, because the size might be 0 and then again
it might be 1 000 000 000 000 items

So is it possble to create an array without defining its size

Rudy Velthuis said:
Martin said:
Hi,

I new to C#, and I would like to know how I can create an array that
I do not know in advance how many item will be placed there

Say you have the number of items in numItems, and you want an array of
integers, then you can do:

int[] myArray = new int[numItems];

--
Rudy Velthuis http://rvelthuis.de

"The secret of success is to know something nobody else knows."
-- Aristotle Onassis (1906-1975)
 
Martin said:
As I was saying I do not know how many items there will be in the
array, so I can not specify the size of it, because the size might be
0 and then again it might be 1 000 000 000 000 items

Oh, you want it to be able to grow? Use a List<T> instead.

FWIW, you will probably have problems with 1 trillion items, no matter
what data type you choose. <g>
--
Rudy Velthuis http://rvelthuis.de

"I think it would be a good idea."
-- Mahatma Gandhi (1869-1948), when asked what he thought of
Western civilization
 
Martin Racette said:
As I was saying I do not know how many items there will be in the array,
so I can not specify the size of it, because the size might be 0 and then
again it might be 1 000 000 000 000 items

So is it possble to create an array without defining its size

This was answered by others, use an ArrayList or Lis<T>, note that you won't
be able to store an array of 1 000 000 000 000 items ever, but I guess you
know that too.

Willy.
 
Thanks, that was exactly what I was looking for

And the number was just an example <g>
 
Thanks, that was exactly what I was looking for

And the number was just an example <g>
 
Rene said:
Maybe this will help?

class Program
{
static void Main(string[] args)
{
// Get an array of ints.
int[] i = GetSomeIntArray<int>(123);

// Get an array of objects.
object[] o = GetSomeIntArray<object>(456);
}

private static T[] GetSomeIntArray<T>(int size)
{
return new T[size];
}
}

This must surely be a solution to a different problem than what OP is
asking for?

1. it is named GetSome*Int*Array, whereas it will happily return an
array of any type, since its generic
2. you can easily replace the whole method with a subset of its
contents, ie:

int[] i = new int[123];
object[] o = new object[456];

What in particular was the point of this method?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top