Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use the ToString() method of array in C#?
The ToString() method in C# returns a string representation of an object. For arrays, the default ToString() method returns the type name rather than the array contents. However, ToString() is commonly used with array properties and methods that return numeric values.
Syntax
Following is the syntax for using ToString() with array methods −
arrayObject.Method().ToString()
For converting array elements to strings −
array[index].ToString()
Using ToString() with Array Properties
The ToString() method is useful when converting array properties like bounds and length to string format for display purposes −
using System;
class Program {
static void Main(string[] args) {
Array arr = Array.CreateInstance(typeof(String), 3);
arr.SetValue("One", 0);
arr.SetValue("Two", 1);
arr.SetValue("Three", 2);
Console.WriteLine("Lower Bound: " + arr.GetLowerBound(0).ToString());
Console.WriteLine("Upper Bound: " + arr.GetUpperBound(0).ToString());
Console.WriteLine("Length: " + arr.Length.ToString());
}
}
The output of the above code is −
Lower Bound: 0 Upper Bound: 2 Length: 3
Using ToString() with Array Elements
You can call ToString() on individual array elements to convert them to their string representation −
using System;
class Program {
static void Main(string[] args) {
int[] numbers = {10, 20, 30, 40, 50};
double[] prices = {15.99, 25.50, 8.75};
Console.WriteLine("Numbers as strings:");
for(int i = 0; i < numbers.Length; i++) {
Console.WriteLine("Element " + i + ": " + numbers[i].ToString());
}
Console.WriteLine("\nPrices formatted:");
foreach(double price in prices) {
Console.WriteLine("$" + price.ToString("F2"));
}
}
}
The output of the above code is −
Numbers as strings: Element 0: 10 Element 1: 20 Element 2: 30 Element 3: 40 Element 4: 50 Prices formatted: $15.99 $25.50 $8.75
Array ToString() vs String.Join()
While ToString() on an array returns the type name, String.Join() is better for displaying array contents −
using System;
class Program {
static void Main(string[] args) {
string[] fruits = {"Apple", "Banana", "Orange"};
Console.WriteLine("Array.ToString(): " + fruits.ToString());
Console.WriteLine("String.Join(): " + String.Join(", ", fruits));
Console.WriteLine("Manual concatenation: " +
fruits[0].ToString() + ", " +
fruits[1].ToString() + ", " +
fruits[2].ToString());
}
}
The output of the above code is −
Array.ToString(): System.String[] String.Join(): Apple, Banana, Orange Manual concatenation: Apple, Banana, Orange
Comparison
| Method | Purpose | Result |
|---|---|---|
| array.ToString() | Returns type name of array | System.Int32[] (type name) |
| element.ToString() | Converts individual elements | String representation of element |
| String.Join() | Joins all elements with separator | Formatted string of all elements |
Conclusion
The ToString() method with arrays is primarily useful for converting array properties (like bounds and length) and individual elements to strings. For displaying entire array contents, String.Join() provides a more practical solution than the array's default ToString() method.
