C# String Array: Use on Variable for Multiple Values

c# pauseArrays are a slightly more advanced concept, but they are a part of any programming language you’re trying to learn. Arrays make it easier to work with multiple values within one variable. Instead of having 10 separate variables for each value you need, you use one array and store all 10 values in this one variable. Before you can get started with these data types, you need to know how to define and work with arrays.

Get started with C# and learn how to work with arrays

Defining a String Array

Before you can work with an array, you must define it. An array is given a specific data type whether it’s a string, integer or decimal. In the case of a string array, you define the variable as a string. What this means to the compiler is that only string values can be stored in the variable. Conversely, if you defined the array as an integer, you’d only be able to store integer values in the array.

The following code is an example of a string array assignment:

string[] myarray = new string[3];

In the code above, a string array is defined with the name “myarray.” The string array is then initialized with 3 indexes. One issue to note is that arrays start with index 0. Therefore, when you define three indexes, the index assignments are 0, 1, and 2 not 1, 2, and 3. This issue is one of the more difficult tricks to understand when learning the C# or any programming language.

You can also define and initialize a string array. While this is not as common, it’s still useful to know when you’re learning how to work with strings. The following code is an example of defining and initializing a string:

string[] colors = new string[3] {"red", "purple", "green"};

Notice the above array has 3 indexes defined and three values assigned with the dimensions. In this example, red would be assigned to the 0 index, purple is assigned to the 1 index and green is assigned to the 2 index. Again, you must remember that indexes start with 0 and not 1 or you will have a hard time when you need to loop through an array.

Looping through an Array

You will commonly need to loop through an array. You usually use a “for” loop to loop through each value. This means you can view each value and then use it to manipulate your data. You might need to loop through an array and store values in your database or you loop through values and display them to your readers.

The following code is an example of looping through a string array with a list of colors:

string[] colors = new string[3] {"red", "purple", "green"};
for (int i = 0; i < colors.Length; i++)
{
Console.WriteLine("The color is “ + colors[i]);
}

In the above code, the integer “i” is used as a way to loop through the color values. The i is initialized to 0. This is important, because your string array is initialized with an index starting at 0. You can technically assign the i any value you want, but if you assigned it the wrong value, you won’t loop through all of your values. For instance, if you assigned the variable to 1, then you’d skip the first value in your array.

The next section in the for loop gets the array’s length. This is important, because you don’t want to increment “i” too much. If you increment the i variable beyond the number of indexes, your code with throw an error and the program will crash. In this example, every time the compiler increments the i variable, it checks to make sure the incremented value isn’t more than the length of the array. If it is, then the for loop will stop running and you know that you’ve incremented through all values. The “i++” statement increments the “i” variable, but only after the first loop.

Understand basic C# and array structures at Udemy.com

This for loop is very simple, because it only prints out each color. Notice the “i” variable replaces the actual number for the array’s index. This is how you dynamically gain access to specific values in an array. In this example, each color is accessed as the “i” variable is incremented. The colors are then printed to the console.

Typically, you do more than just print out values in an array. You will usually use “if”  statements and other logic operators to manipulate the data and change values in the array or in other areas of your GUI.

You can also replace values in the array.  For instance, if you want to replace the color purple with the color pink, you can use the following loop:

string[] colors = new string[3] {"red", "purple", "green"};
for (int i = 0; i < colors.Length; i++)
{
if (colors[i] == “purple”)
{
colors[i] = “pink”;
}
Console.WriteLine("The color is “ + colors[i]);
}

The above code iterates through each color and when the color “purple” is found, the code replaces the existing array element with the new color pink.

This is just an example of the string array in C#, but there are many other array types you can use when you work with C#. For example, you might need to work with an array of integers. If you replace the integer values and the integer data type with the string data type and values, you can use the same code to iterate through a list of integers.

C# also has advanced classes and structures you can use to make working with arrays easier. For instance, the ArrayList class has several methods and properties that help you work with arrays. However, it’s good to know the basic array structure to truly understand more advanced classes and structures.

Learn more about C# and its functionality at Udemy.com