C# Programming > Data

Submit a Comment

C# Array To String

 

C# Array and Lists

It's tricky to write a good C# function to convert any array to string 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 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 an ArrayList, which allows programmers to create a list of elements of different types. It was widely used in C# 1.1.

A generic list is any collection that is typed, like List<> for example. Generics are new to C# 2.0 and are seriously 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

I won't bore you with the actual function, since it can be downloaded at the bottom of the page and it is not the important part (it is just printing each element out with a for-loop).

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)

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 is good programming practice to use the second call.

Non-Generic Array to List

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 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

Download Convert Array to String C# Source Code

Back to C# Article List

License Agreement | Privacy Policy
Link to Us | Subscribe to Newsletter | Contact Us