-3

Why does the following code print System.Int32[] in the console and not the values in the array? In simple words?

static int[] minMax(int[] lst)
{
    int a = lst.Min();
    int b = lst.Max();
    return new int[] { a, b };
}
System.Console.WriteLine(minMax(new int[] { 1, 2, 5, -1, 12, 20 }));
Rand Random
  • 7,300
  • 10
  • 40
  • 88

2 Answers2

0

What you probably want is Console.WriteLine(string.Join(‘,’, array));.

String.Join() takes a collection, and adds all the elements with a character between them.

SupaMaggie70 b
  • 322
  • 3
  • 8
-1

WriteLine will call .ToString() on an object and output the value returned by that method.

By default, .ToString() for simple objects (int,string etc) will return the value, and for complex objects (list, class etc) it will return the type.

If you want to override the default, then you can override string ToString() with whatever you want.

Neil
  • 11,059
  • 3
  • 31
  • 56
  • `In simple words?` – Rand Random Nov 21 '22 at 19:33
  • 1
    Some example of "you can override string ToString() with whatever you want." for arrays would be actually useful (I don't really see how recommendation can be applied to the question)... otherwise no need to answer well known duplicates. – Alexei Levenkov Nov 21 '22 at 21:02