Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
I Don't Know What Size Of Array is?

If I Am Creating an Array:
C#
int[] marks = new int[10];


In This Case If User Want Add More Than 10 , It Will Throw Error

And I Can't Use This One Also
C#
int [] marks = new int[]  { 99,  98, 92, 97, 95};
Posted
Updated 8-Nov-15 23:54pm
v2

In addition to Solution 1:

Volkan Paksoy meant the generic class System.Collections.Generic.List<>:
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

Depending on what operations you really need to support, you may need other types of collections, but don't use obsolete ArrayList.

Now, closer to what you wanted to do you can do this, but don't:
https://msdn.microsoft.com/en-us/library/bb348051%28v=vs.110%29.aspx[^].

Why "big bold don't"? Read "Remarks" section. It would create a brand new array object each time you reallocate it, amd copy all data, but the effect would be miserable, at this cost, it is not even functionally acceptable. You have to decide how many elements to add; and it's bad to face a need for an arbitrary decision; then you add 10 and the user only uses 1, besides, you will have to maintain the number of actually uses elements, as opposed to allocated. Collections, collections, only collections.

By a similar reasons, never repeatedly concatenate "the same string", it's not the same. String is immutable. System.Text.StringBuilder, which can be considered as a mutable analog of string.

—SA
 
Share this answer
 
You can use a List<int> instead of an array like this:

C#
var myList = new List<int>();


You can add or remove as many items you want at runtime. If you definitely need to get an array out of it you can convert it with ToArray method:

C#
var myArray = myList.ToArray();
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Nov-15 10:07am    
Yes, this is what the inquirer really needs (up-voted the answer), but I added some explanation and information, please see Solution 2.
—SA
Well, you cant.
But you can convert your array to list and then append item to it.

Something like,
C#
int[] marks = new int[10];
// fill your array with the values
List<int> MarksList = new List<int>(marks);
marksList.Add(75);

Use this List<t> Constructor[^]

[Edit]
<int> tags removed

-KR
 
Share this answer
 
v2
Comments
Philippe Mori 5-Nov-15 12:28pm    
Why create an array? Use only a list (except if you already have some data that come in that format from elsewhere).
Krunal Rohit 5-Nov-15 23:37pm    
Yes, but what if there's a dynamic change ?
Well, this is an alternative solution :)

-KR
Hello ,

Array.Resize function is there.

Try This,

C#
int[] marks = new int[10];
Array.Resize(ref marks,marks.length+1);



It will be Resize your array to +1 from the current length.
 
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