Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Creating an array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Racette - 17 Jan 2008 19:24 GMT
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

Signature

Thank You in Advance
Merci a l'avance

Martin

Jeroen Mostert - 17 Jan 2008 19:31 GMT
> 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".

Signature

J.

Rene - 17 Jan 2008 19:50 GMT
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];
   }
}

> 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
Lasse Vågsæther Karlsen - 19 Jan 2008 13:27 GMT
> Maybe this will help?
>
[quoted text clipped - 14 lines]
>     }
> }

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?

Signature

Lasse Vågsæther Karlsen
mailto:lasse@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3

Rudy Velthuis - 17 Jan 2008 20:00 GMT
> 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];

Signature

Rudy Velthuis        http://rvelthuis.de

"The secret of success is to know something nobody else knows."
-- Aristotle Onassis (1906-1975)

Martin Racette - 18 Jan 2008 15:34 GMT
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

>> Hi,
>>
[quoted text clipped - 5 lines]
>
>  int[] myArray = new int[numItems];

Signature

Thank You in Advance
Merci a l'avance

Martin

Rudy Velthuis - 18 Jan 2008 16:06 GMT
> 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>
Signature

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 - 18 Jan 2008 17:18 GMT
Thanks, that was exactly what I was looking for

And the number was just an example <g>

>> 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
[quoted text clipped - 4 lines]
> FWIW, you will probably have problems with 1 trillion items, no matter
> what data type you choose. <g>

Signature

Thank You in Advance
Merci a l'avance

Martin

Willy Denoyette [MVP] - 18 Jan 2008 16:09 GMT
> 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.
Martin Racette - 18 Jan 2008 17:18 GMT
Thanks, that was exactly what I was looking for

And the number was just an example <g>

>> 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
[quoted text clipped - 7 lines]
>
> Willy.

Signature

Thank You in Advance
Merci a l'avance

Martin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.