c sharp

C# String Arrays

The string array in C# programming is the topic of this Linux Hint tutorial. So, we will first talk about what an array is. The array is like a container that saves the values of a similar type. Arrays collect the data that we store in multiple variables of one type in one place. It stores the elements starting at the index 0. The string is a collection of characters that are stored in sequence. When more than one character is stored in a memory location, it is called a string. Arrays of string are used to store a fixed number of strings. We can store more than one string in the array. The string contains multiple characters, and the string array contains numerous strings. When we have to store students’ names, we can utilize the string array rather than storing the names by declaring separate variables for each student. The string array can be single or multiple like other arrays, i.e., integer or float. We must set the array’s size first to use a string array.

Declaration of String Arrays

Two techniques can declare the string arrays.

Declaration with size:

String[] var_1 = new String[3]

or

string[] var_1 = new string[3]

The difference between both statements is the use of string. In the first statement, the “String” is the String class object utilized to create objects. Whereas the other “string” is used to create a string, it is a keyword. Here, 3 is the size of the string array.

Declaration without size:

String[] var_1;

or

string[] var_1;

Both statements will create a string. However, we are not defining the size of the string. The sting is just declared.

Initialization of String Arrays

The array cannot be initialized until its size is set. We can use two techniques to initialize an array.

Initialize while declaring:

String[] var_1 = new String[2]

or

string[] var_1 = new string[4]

We can initialize an array when we declare the data type of a variable.

Initialize after declaring:

String[] var_1;

var_1 = new String[3];

The size of the array can be defined after the variable declaration.

Value Assigning

The values of string type array can be assigned when we declare and initialize an array type variable. And the other way is to assign the values by index number.

Assign all in curly braces:

String[] var_1 = new String[3]{ “john”, “jerry”, ”salt” };

Assign with index:

String[] var_1 = new String[3];

var_1[0] = ”john ”;

Example No. 1

This example code will define the string array using the class object String.

using System;

class Program_1
{
    static void Main() {
String[] nick_names = new String[3]{ "Nasline", "Vali", "Micki" };
Console.WriteLine("The Nick Names are: ");
Console.WriteLine(nick_names[0]);
Console.WriteLine(nick_names[1]);
Console.WriteLine(nick_names[2]);
    }
}

We have to initialize a string array “nick_names” and store the values. The array size is three, and this array contains students’ nicknames. The string array is declared by creating an object of the String class. The “nick_names” is an object of the class “String”. Double quotes surround the string values. The values are assigned to the array while the variables are declared and initialized. It is the one way to assign values. Then print a message “The Nick Names are” on the screen using Console.WriteLine() function. To access a specific value of an array, we call that value by its index. Arrays are stored on different indexes. The starting index is 0. Index 0 stores the first element of the array, and so on. To print the elements of the array on the console, we will call the variable with its index inside the Console.WriteLine() method.

Example No. 2

In this scenario, we will use the “string” word to define a string array.

using System;

class Program_2
{
    static void Main() {
string[] fruits = new string[4]{ "Apple", "Pear", "Mango", "Pineapple" };
Console.WriteLine("The Names of fruits are:\n ");
for(int i=0; i<fruits.Length; i++)
       {
Console.WriteLine(fruits[i]);
       }
    }
}

We will declare the string array “fruits” to save the names of several fruits. The size of the required array is 4, which means it can hold four elements in it. Then assign the values to the string array. Next, we will display the text “The Names of fruits are” on the screen using the Console.WriteLine() function. Then we will apply a “for” loop. We cannot display the complete array with its name. To show the whole array, we need to use the “for” loop to loop the string until its length. Define an iterator with value 0 and specify the condition on the array's length. Here, we also acquire the length of the string array by using the “var_name.Length” method. The function in the body of the “for” loop would be executed equally multiple times as the loop itself. The body of the “for” loop has one statement, and each time it is executed, the value of “i" will be placed inside the fruits[i]. When i=0, the name of the fruit on the index 0 (fruits[0]) will be presented on the screen, and so on. By doing this, every element of the string array would be shown on the terminal.

Example No. 3

Index numbers, in this instance, assign the elements of the string array.

using System;

class Program_3
{
    static void Main() {
string[] menu;
       menu = new string[3];
menu[0]="Pasta";
menu[1]="Pizza";
menu[2]="Burger";

Console.WriteLine("The Menu is as under: ");
for(int i=0; i<menu.Length; i++)
       {
Console.WriteLine(menu[i]);
       }
Console.WriteLine("Yummmm...");
    }
}

The string array “menu” initialization is done using the index. Then define the size of this string array. To assign the values use the variable name “menu” and assign the index within the square braces. In the next statement, employ the Console.WriteLine() method to print a text on the console. Next, the “for” loop represents the entire array on the terminal. Within the loop, the condition is defined for the array's length and increments the iterator by 1. The Console.WriteLine() function is invoked in the body of the “for” loop, which allows the items of the string array to be depicted on the screen. Outside the loop, another statement, “Yummmm…” will be printed on the screen by calling the Console.WriteLine() method.

Conclusion

In this guide, we have examined the basics of arrays, what string arrays are, and how the string arrays are used in codes of the C# language. Different techniques of declaring, initializing, and assigning the values to the string array are also elucidated. We also observed the usage of string arrays in numerous coding examples. A string array can be declared using the String class object or the word “string”. Both techniques are equally effective, but they use different methodologies to accomplish the same tasks.

About the author

Ann-ul Hayyat

I'm a professional SEO and technical content writer with extensive experience in these fields as well as writing content for blogs, websites, reports, and other topics related to education, science, sports, and many other areas. I am skilled and experienced in creating SEO-friendly blog posts, technical articles, and other content on themes that you may find interesting.