C# beginner- Question regarding returning of Array values

internal double[] fillingArray() {     a = 1.5;     b = .5;          for (i = 0; i<10; i++)     {         c = a * b;         a = a + 1;         b = b + 1;         arrayInt[i] = c;       }          return arrayInt; } 

I wanted to call a method fillingArray() which stores array variables and display those variables via displayAll() main method. When arrayInt is returned, console displays system.Double but not the values inside the array.

Add Comment
3 Answer(s)

You need to parse each value like with the filling function:

static void Test() {   Display(CreateFilledArray()); }  static internal void Display(double[] array) {   foreach ( var value in array )     Console.WriteLine(value); }  static internal double[] CreateFilledArray() {   var result = new double[10];   double a = 1.5;   double b = .5;   for ( int index = 0; index < 10; index++ )   {     result[index] = a * b;     a = a + 1;     b = b + 1;   }   return result; } 

Output

0,75 3,75 8,75 15,75 24,75 35,75 48,75 63,75 80,75 99,75 
Add Comment

I get the feeling that you’re doing something like this:

Console.WriteLine(arrayInt); 

And expecting it to show a representation of all the numbers in the array. C# doesn’t do that; it will just print out the type of the array, which is System.Double[]

what is actually happening here is that WriteLine can only write "sensible looking things" it if it’s something it can specifically handle. WriteLine can handle lots of different things:

Console.WriteLine(1); //handles numbers Console.WriteLine('1'); //handles characters Console.WriteLine("1"); //handles strings 

WriteLine cannot handle arrays; they get passed to the version of WriteLine that handles Objects, and the only thing that that version of WriteLine does is call ToString() on the object that is passed in and then print the string result that ToString returns. When we call ToString on an array, it just returns the kind of array it is, not the values in the array

This is why you see "System.Double[]" appear in the console

Youll have to loop over the array printing the values out one by one (there are other tricks you can do, but they’re more advanced c# than you know currently and it would not be wise to recommend them if this is an academic assignment)

I can see you know how to loop, so I’ll leave the implementing of it up to you

Answered on July 16, 2020.
Add Comment

I modified the method to string and returned a string value concatenating values of array — this worked

internal string fillingArray()         {             a = 1.5;             b = .5;             for (i = 0; i<10; i++)             {                 c = a * b;                 a = a + 1;                 b = b + 1;                 arrayInt[i] = c;                 //astr array string                 astr = astr + arrayInt[i].ToString() + " ";             }             return astr; 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.