.ToString
on an array does not stringify its contents CS-W1020Array
9 // CS-W1020: Calling `.ToString()` on an array does not stringify it.
10 // https://deepsource.io/directory/analyzers/csharp/issues/CS-W1020
11 var arr = new[] {1, 2, 3, 4, 5};
12 Console.WriteLine($"Array is {arr.ToString()}");13
14 var name = "Joe";
15 // CS-W1000: Missing arguments to interpolation.
The general expectation is that the .ToString
method return an appropriate string
that best represents an entity. However, calling the said method on an array does not stringify its contents. Rather, it returns a string that represents its type. If your intention is to stringify the array, it is suggested that you use other alternatives such as string.Join()
.
Console.WriteLine(new int[]{1, 2, 3, 4, 5}.ToString()); // Prints `System.Int32[]`
Console.WriteLine(String.Join(", ", new int[]{1, 2, 3, 4, 5})); // Prints `1, 2, 3, 4, 5`