Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I am having a string array like string[] nums={1,2,3............,34}
I need 10 elements from that array each time until the length exceeds.How can i do that using c#
Posted
Comments
phil.o 4-Sep-15 10:43am    
What have you tried?
Normally, an array has a fixed size, and it is time-consuming to change this size after the initialization.
I would rather use a List<string> instead, that would allow you to add/delete elements on the fly without having to worry about the size of the collection.

1 solution

Arrays are not designed for adding or removing elements. All methods changing the size of array (https://msdn.microsoft.com/en-us/library/bb348051(v=vs.110).aspx[^]) don't actually modify an array object; they create a new, populate it with the data from old array and return a new array object, not referentially identical to the original one.

For such operations, you need to use collections, with the closest to the array being System.Collections.Generic.Array<&ft;:
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

You can always make an array from a list instance: https://msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx[^].

—SA
 
Share this answer
 

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