C# Programming > Data

C# Array To String

 

C# Array and Lists

It's tricky to write a good C# array to string function but it is possible. Array referring to arrays, lists, and generic lists. C# programmers have grown used to writing simple C# functions to convert an array to string for each application they write.

In this article we are going to take the time to write a solid string function to convert any type of array to string. (Similarly you should check out how to convert a string back to an array.)

c# array to string

Generic vs Non-Generic

First we must differentiate between a generic list and a non-generic list. A non-generic list is a list of objects that are not type-checked at compile time. In the case of C#.NET, it is an ArrayList. It was widely used in C# 1.1.

A generic list is any collection that is typed, like List<> for example. Generics were introduced in C#.NET 2.0 and are the way to go.

The difference between the types of list is relevant because the two lists inherit a different interface. ArrayList inherits IList while generics inherit IList<>.

It might not seem like a big difference, but it actually makes it impossible to write a single function for ALL types of list. We are going to have to write two C# functions: one for the old non-generic lists, and one for generic lists.

Generic Array to String

The important part is the definition of the function. Since generic lists can be of any type, we have to make use of the notation <T> to let the compiler know to expect a single type of elements:

public static string ArrayToStringGeneric<T>(IList<T> array, string delimeter)
{
    string outputString = "";

    for (int i = 0; i < array.Count; i++)
    {
        if (array[i] is IList<T>)
        {
		    //Recursively convert nested arrays to string
            outputString += ArrayToStringGeneric<T>((IList<T>)array[i], delimeter);
        }
        else
        {
            outputString += array[i];
        }

        if (i != array.Count - 1)
            outputString += delimeter;
    }

    return outputString;
}

This function is very elegant. It will work for any type of C# list (including arrays such as int[]). The only list it will fail to compile with is ArrayList. That is because ArrayList is non-generic.

Another great aspect of the string function above is that you don't need to specify T. Both of these calls will compile:

ArrayToStringGeneric(myArray);
ArrayToStringGeneric<int>(myArray);

Where myArray is an int[] variable. Note that the second call is argueably better since it makes it clearer what type of array is being passed.

Non-Generic Array to String

To make the function convert an ArrayList to string, you need to convert it to non-generic:

public static string ArrayToString(IList array)

Technically this function will work for ANY type of list, including generic ones and simple arrays. If you do not want to bother with generics vs non-generics then this will be the string function for you. The downside is that it is not as clean or elegant as its generic counterpart.

Source Code

As promised, you can download the C# array to string functions (generic and non-generic) below. Used in combination, the functions will convert any array to string without problem. Also take a look at the Visual C# Kicks String Processing Library for other powerful text functions.

String Processing Library PRO

Back to C# Article List