String arrays allow storing multiple strings together. There are several equivalent ways to declare and initialize string arrays, including initializing elements directly or separately assigning values later. String arrays can be used as fields within classes and accessed via properties or indexers. Loops like foreach and for can be used to iterate over the elements of a string array.
String arrays allow storing multiple strings together. There are several equivalent ways to declare and initialize string arrays, including initializing elements directly or separately assigning values later. String arrays can be used as fields within classes and accessed via properties or indexers. Loops like foreach and for can be used to iterate over the elements of a string array.
be stored together. One way to do this is with a string array. Several different syntax forms are used to declare and create string arrays. They are often compiled into equivalent code. String Example First, there are several ways to declare and instantiate a string array local variable. 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. Note: All of these ways are equivalent in the compiled code, so please choose the one you think is clearest or easiest to read. 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"; } } Go C#: Array C# Classes C# Csharp VB Net Result Four string arrays are initialized. 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. 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 Instruction stelem 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. 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 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. 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 Empty 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 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 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 Array Foreach For Summary 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: In these situations, a good technique is to return empty string arrays. This avoids excess complexity. UK IT Degrees RDICaribbean.com Apply to study with RDI. Affordable distance learning. Apply now! Programming in C# C# Code C# Tutorial