Click here to Skip to main content
15,911,315 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
for example
C#
int[] x;
x[5] = console.readline();
x[20] = console.readline();

without doing this
C#
int[] x = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
x[5] = console.readline();
x[20] = console.readline();

is there a command for this?
Posted
Updated 29-Apr-14 9:01am
v2
Comments
[no name] 29-Apr-14 15:05pm    
Not as far as I know.
Herman<T>.Instance 29-Apr-14 15:10pm    
read this
[no name] 29-Apr-14 15:36pm    
If you just give your int array a size, it will be initialized to all 0 anyway.

You can simply declare the size of your array:
C#
int[] x = new int[31];


Though, I can see a major flaw in your code: your are trying to assign a string value (the result of the ReadLine() method) to an int variable (because your array is an array of integers - Int32).

You should better write:
C#
int value;
string input;
do {
   Console.Write("Enter x[5]: ");
   input = Console.ReadLine();
   if (int.TryParse(input, out value)) {
      x[5] = value;
   }
   else {
      input = null;
   }
while (string.IsNullOrEmpty(input));
do {
   Console.Write("Enter x[20]: ");
   input = Console.ReadLine();
   if (int.TryParse(input, out value)) {
      x[20] = value;
   }
   else {
      input = null;
   }
while (string.IsNullOrEmpty(input));

This code is if you absolutely require that values should be entered at given indices. If you do not, then you can remove the do..while construction.

Oh, by the way, C# is Case Sensitive :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Apr-14 16:29pm    
Right, a 5.
—SA
Sergey Alexandrovich Kryukov 29-Apr-14 16:37pm    
Please see also Solution 3 I added to explain immutable array size and fix Solution 2.
—SA
C#
int[] x = new int[size];

You must know the size of the array.
If you want something that sort of works like an array that grows automagically as items are entered then you can use:
C#
Dictionary<int,int> x = new Dictionary<int,int>();

However it will still throw an exception on references to items that were never set.

EDIT: based on comments and other Solutions:
Re: default value
The simplest way to do this for the int[] is using the Enumerable methods (from Linq):
C#
int[] x = Enumerable.Repeat(your_default_value, number_of_elements_in_array).ToArray();
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 29-Apr-14 16:32pm    
No, first line is incorrect: remove '()';
Dictionary is not the only way of getting this functionality: there are two more key-value pair based types, but they are all redundant for the purpose: as the index OP requires is always int, simple System.Collections.Generic.List<> is more adequate and will give better performance with less memory overhead.
—SA
Matt T Heffron 29-Apr-14 16:41pm    
Yes, the () are a think-o.
List<> does NOT provide the automatic grow to size to include an element referenced that the use of Dictionary<int,int> would simulate, USING the [index] syntax that looks like an array.
.Insert() will grow the collection as necessary.
Sergey Alexandrovich Kryukov 29-Apr-14 17:06pm    
I agree, List it does not provide this feature, but it does not matter if the solid index range is used. Dictionaries allows for a kind of sparse array; non index — no data. So, the point it taken; this can be what OP wants.
—SA
Sergey Alexandrovich Kryukov 29-Apr-14 16:37pm    
Please see also Solution 3 I added to explain immutable array size and fix your solution.
—SA
In addition to Solution 2 and to fix it (please see my comment to it): if you really need a type with variable data size, use System.Collections.Generic.List<string>:
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

Note that the size of .NET arrays is immutable. Despite the method name, System.Array.Resize does not actually resize array; it creates a brand-new instance of the array with the same data but different size (hence ref method of passing of this parameter): http://msdn.microsoft.com/en-us/library/vstudio/bb348051[^].

[EDIT]

Solution 2 is still good if you need some "sparse array". If some indices are not used, there is no data for them. But this is not only dictionary. Look at these generic types:
System.Collections.Generic.Dictionary,
System.Collections.Generic.SortedDictionary,
System.Collections.Generic.SortedList.

—SA
 
Share this answer
 
v3
Comments
phil.o 30-Apr-14 2:54am    
Thank you Sergey. I did not take into account the default value part (must confess I did not even see it), as with valuetypes a default value is never a problem.

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