Click here to Skip to main content
15,883,805 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have 1 trouble about working with array.When initialize 1 array, we must declare the length of array.
Sometimes,the length of the array will not be known, so how to initialize the array ?
I'm using Visual Studio 2005 with .Net Framework 2.0 , languague C#.
Please help me.
Posted
Updated 8-Jun-11 4:32am
v2

1 solution

When you declare an array, the length has to be known, as the array is considered as a contiguous block of memory, which has to be allocated together.
If you want to use an array where you do not know the length until you have filled it, consider using a List<T> instead and use the Add method to just keep adding elements - you can use the ToArray method later to get the array content if you need it.
 
Share this answer
 
Comments
SaoLamEmDau 8-Jun-11 9:20am    
Thanks for help!
Have it similar as in Java ?
OriginalGriff 8-Jun-11 9:24am    
Sorry - I don't know enough about Java to comment, but I would imagine it is much the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900