-1

When I make a function to return an array with the correct results. Instead of giving me the correct results, I get as result System.Int32[]. Anyone an idea why this is?

    class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(MultiplyByLength(new int[] {2,3,1,0}));
    }

    public static int[] MultiplyByLength(int[] arr)
    {
        return arr.Select(x => x * arr.Length).ToArray();
    }
}
  • 3
    Does this answer your question? [System.Int32\[\] displaying instead of Array elements](https://stackoverflow.com/questions/18033938/system-int32-displaying-instead-of-array-elements) – devNull Aug 24 '20 at 00:38
  • What result do you **want**? – mjwills Aug 24 '20 at 01:03

1 Answers1

4

You need to format it some how. An array doesn't have a ToString() override that knows how you want to format your type (int[]) to a string, in such cases it just returns the type name (which is what you are seeing)

foreach(var item in MultiplyByLength(new int[] {2,3,1,0})
    Console.WriteLine(item);

or

Console.WriteLine(string.Join(Environment.NewLine, MultiplyByLength(new int[] {2,3,1,0}));

or

Console.WriteLine(string.Join(",", MultiplyByLength(new int[] {2,3,1,0}));
Dmitry
  • 13,797
  • 6
  • 32
  • 48
TheGeneral
  • 79,002
  • 9
  • 103
  • 141