C# Programming > Data

C# String To Array

C# Strings

Sometimes it is necessary to convert a string to an array in C#. With the .NET Framework the source code is relatively simple without worrying about the string parsing algorithms too much.

Convert String to Array

To convert a string into a .NET array we need two pieces of information (additional to the actual string of course). The first is the separator between elements. Many times the separator will be something like a comma, but not always. The second thing is the type of the output array.

The second thing is particularly important. When building the array, each element in the string should be cast to the appropriate type.

The C# Function

The code is a lot cleaner if you know exactly what type of array you want to convert the string to. However we can attempt to write a general C# function to convert a string into an array, given the "target" type of the elements.

The easiest way to go about it is to make use of the string Split function. Calling the Split method on a string separates the text into a string array. The function takes an array of characters that indicate the separator. So for example, consider the following code to split a simple string list:


The resulting array would of course be ["1", "2", "3", "4"]. Notice that the elements are strings. If you want an array that is made up of strings that is fine. Otherwise you have to cast the elements to the proper .NET type.

Casting can be very simple or very complicated. In fact this is where the generalized function gets messy. If you know what type of the string elements are going to be converted to, better to write a concrete function.

Otherwise we can make use of the Convert.ChangeType() function. The ChangeType function will take in an object, which in this case will be our string element, and the "target" type, an attempts to cast the object.

However since this is a general C# function, as in we don't know ahead of time the return type of the array, the resulting array will be an object[] array. So when accessing elements in the object[] array later, they will have to be explicitly cast into the target type. However it is not a complete waste of time because explicitly casting and using ChangeType are not the same thing. For example, (int)"1" will not work but (int)Convert.ChangeType("1", typeof(int)) will.

The Code

Putting the ideas together yields a C# function which looks something like this:

public object[] StringToArray(string input, string separator, Type type)
    string[] stringList = input.Split(separator.ToCharArray(), 
    object[] list = new object[stringList.Length];

    for (int i = 0; i < stringList.Length; i++)
        list[i] = Convert.ChangeType(stringList[i], type);

    return list;

Of course, converting a string to an array in .NET can be done more efficiently and more cleanly by knowing ahead of time what type array you want. Going the other way, converting an array to string, is similar.

Back to C# Article List