C# String Arrays

String array

Strings sometimes need to be stored together. One way to do this is with a string array. Several syntax forms are used to declare and create string arrays. They are often compiled into equivalent code.

Strings

Example

There are several ways to declare and instantiate a string array local variable. We show them. In the C# language, a string array is of type string[]. The reference can be null or assigned to a new string[]. Square brackets are used.

Tip

Note:All of these constructs are equivalent in the compiled code. Please choose the one you think is clearest.

Var:The var type is an implicitly-typed local variable. It is the same to the compiler as an explicit string array.

Var
Based on:

.NET 4.5

Program that initializes string arrays: C#

class Program
{
    static void Main()
    {
	// String arrays with 3 elements:
	string[] arr1 = new string[] { "one", "two", "three" };
	string[] arr2 = { "one", "two", "three" };
	var arr3 = new string[] { "one", "two", "three" };

	string[] arr4 = new string[3];
	arr4[0] = "one";
	arr4[1] = "two";
	arr4[2] = "three";
    }
}

Result
    Four string arrays are initialized.
Main method

The above Main function shows four string arrays, each equivalent to the compiler. The biggest difference is that the first three arrays are declared on one line, while the fourth array is assigned in separate statements.

Tip:The fourth array would allow you to test each value or insert logic as you assign it. This is sometimes useful.

Abstract squares

The above initializations each result in several intermediate language instructions. To create the array reference, "newarr string" is emitted. And then to assign each element the instruction "stelem" is used.

newarr Instructionstelem Instruction

Fields

Next we use string arrays as fields or properties in classes.
This is useful for storing values,
either statically
or in instances.
You can also return string arrays,
with methods
or indexers.

Array type

Next:The first part of the code is the Main method, which is the standard program entry point.

Then:A new instance of the Test class is created.
That class contains an array of strings.

Program that uses string array: C#

class Program
{
    static void Main()
    {
	// Create new instance with string array.
	Test test = new Test();

	// Loop over elements with property.
	foreach (string element in test.Elements)
	{
	    System.Console.WriteLine(element);
	}
	System.Console.WriteLine(test[0]); // Get first string element
    }
}

public class Test
{
    /// <summary>
    /// String array field instance.
    /// </summary>
    string[] _elements = { "one", "two", "three" };

    /// <summary>
    /// String array property getter.
    /// </summary>
    public string[] Elements
    {
	get { return _elements; }
    }

    /// <summary>
    /// String array indexer.
    /// </summary>
    public string this[int index]
    {
	get { return _elements[index]; }
    }
}

Output

one
two
three

one
String type

In this example, the Test class contains a string array field. The elements in the array are actually added in the constructor for the class, which the C# compiler generates automatically.

The second part of the Test class is a property accessor. It provides a clean way for external code to access the internal array. Properties are not useful in many cases, but in some systems can help.

This keyword

Indexer:The final part of the Test class is called an Indexer.
An indexer uses the this-keyword.

Note:This is a function that receives one parameter, an integer, and returns a value based on that parameter.

Indexer

Null property

Property

As you will see on the MSDN array usage guidelines, returning a null string array in a property can be confusing to callers. It is therefore discouraged. We examine this issue with array properties, and review also MSDN.

Array Property, Return Empty Array

Types

Squares: abstract

This article focuses on string arrays, and string arrays are different from other arrays in some ways. Strings are the only data type you can declare with quoted values. Also, strings are reference types and can be null, unlike integers.

Even though strings and string arrays are different in their syntax, the general usage remains the same. You could substitute int arrays or object arrays and the balance of the program would not change much.

Convert:A common problem with string arrays is converting them to other types of arrays and strings. Custom methods are often used.

Tip:This site has more detailed information about converting string arrays to strings, and char arrays and strings.

Convert String Array to String

Loops

For loop

To continue, you can loop over string arrays in your C# programs. There are two main ways to loop over string arrays: the foreach and for-loops. These loop constructs are covered in depth in separate articles.

Loop Over String ArrayForeachFor

Summary

The C# programming language

We declared string arrays in several different ways. These syntax forms are often compiled to similar code. It is effective to use string arrays in return values and also in indexers.

Tip:A good technique is to return empty string arrays.
This avoids excess complexity.


C#: Array