Codester2020
Codester2020

Reputation: 9

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.

Upvotes: 0

Views: 209

Answers (3)

Codester2020
Codester2020

Reputation: 9

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;

Upvotes: 0

Caius Jard
Caius Jard

Reputation: 74625

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

Upvotes: 1

user12031933
user12031933

Reputation:

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

Upvotes: 2

Related Questions