Printing the array will call the ToString() method on the array, and it will print the class name, which is the default behavior. To overcome this problem, we usually redefine the ToString function of the class.
According to the discussion here, we cannot override Array.ToString () instead of List may be useful.
Simple and direct solutions have already been proposed, but I would like to make your life even easier by inserting functionality into Extension Methods (framework 3.5 or higher):
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 to the 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);
I hope you will be very helpful.
NOTE. It should work for all data types due to the object type. I tested several of them.
NeverHopeless Aug 04 '13 at 10:25 2013-08-04 10:25
source share