Printing an array will call the ToString()
method of an array and it will print the name of the class which is a default behaviour. To overcome this issue we normally overrides ToString
function of the class.
As per the discussion here we can not override Array.ToString() instead List
can be helpful.
Simple and direct solutions have already been suggested but I would like to make your life even more easier by embedding the functionality in Extension Methods (framework 3.5 or above) :
public static class MyExtension
{
public static string ArrayToString(this Array arr)
{
List<object> lst = new List<object>();
object[] obj = new object[arr.Length];
Array.Copy(arr, obj, arr.Length);
lst.AddRange(obj);
return string.Join(",", lst.ToArray());
}
}
Add the above code in your namespace and use it like this: (sample code)
float[] arr = new float[26];
for (int i = 0; i < 26; i++)
arr[i] = Convert.ToSingle(i + 0.5);
string str = arr.ArrayToString();
Debug.Print(str); // See output window
Hope you will find it very helpful.
NOTE: It should work for all the data types because of type object
. I have tested on few of them