Why am I printing out 'System.Int32[]' instead of my array of numbers?

3.5k views Asked by At

I am trying to print out my array of numbers that I have assigned to a particular array. My algorithm for choosing numbers consists of choosing a random number that is not a duplicate and storing it inside the array.

Pretty simple really, but I have no idea as to why it is printing out this error.

int[] ticket1 = new int[4];
for (int i = 0; i < 4; i++)
{
    int temp = rand.Next(43);
    while (ticket1.Contains(temp)) 
    {
        temp = rand.Next(43);
    }
    ticket1[i] = temp;
}
Console.WriteLine("{0}{1}", item.PadRight(20), ticket1.ToString());//ticket1 produces System.Int32[] instead of 4 numbers.
//I have changed this line to:
//Console.WriteLine("{0}{1}", item.PadRight(20), string.Join(",", ticket1));
//And it still doesn't work. the error remains. (System.Int32[])

My question is, how can I print out my 4 numbers (beside each other) in string format.

//EDIT: I've found my problem. I am putting my ticket1 inside a foreach loop, it's somehow not reaching out to the array values and it therefore prints out System.Int32[] instead.

All fixed.

4

There are 4 answers

1
Grant Winney On BEST ANSWER

If you call ToString() on an array like that, you simply get the full name of the type of class.

You could fix it a few ways. Print only the current item inside the loop, or print each item one at a time outside of the loop:

Console.WriteLine("{0}{1}", item.PadRight(20), ticket1[0]);
Console.WriteLine("{0}{1}", item.PadRight(20), ticket1[1]);
// etc...

Or "flatten" the collection before printing:

Console.WriteLine("My numbers: ", String.Join(", ", ticket1));
2
Soner Gönül On

Because you are not writing your array elements, you are writing your array itself, that's why ToString() generates it's full type name.

Change your ticket1.ToString() to ticket1[i] in your for loop.

for (int i = 0; i < 4; i++)
{
     int temp = rand.Next(43);
     while (ticket1.Contains(temp))
     {
         temp = rand.Next(43);
     }
     ticket1[i] = temp;
     Console.WriteLine("{0} {1}", item.PadRight(20), ticket1[i]);
}

If you don't want to print it inside your for loop, then you can use String.Join to concatenate all your elements in your array in a simple string like;

for (int i = 0; i < 4; i++)
{
     int temp = rand.Next(43);
     while (ticket1.Contains(temp))
     {
         temp = rand.Next(43);
     }
     ticket1[i] = temp;
}
Console.WriteLine("{0} {1}", item.PadRight(20), string.Join(",", ticket1));
0
Sergey Kalinichenko On

ticket1.ToString() does not print the content of the array, only its type, because this is the way the ToString() method is implemented on arrays.

You can fix this in several ways - for example, by using string.Join method:

Console.WriteLine("{0}{1}", item.PadRight(20), string.Join(",", ticket1));
0
David On

Because when you call .ToString() on an object you get the type of that object. For basic primitive (value) types this behavior is overridden to output the value. But for something like an array there's no "default" string representation, so the behavior you're seeing is the default.

You could wrap your data in an object and override .ToString() on that object. If you have to output the values in many places in the code that would be the way to go so you only have to write the logic once. ("Smart data structures and dumb code works a lot better than the other way around." - Eric Raymond)

But if you only need to do it here then you can just output the values directly. Basically join the values as a string in whatever representation you want. For example, if they should be comma-separated:

Console.WriteLine(
  "{0}{1}",
  item.PadRight(20),
  string.Join(",", ticket1));