Creating string array in C#

A string array in C# can be created, initialized, and assigned values as described below.

Declaring a string type array:

string[] strArr;

Initializing the string array:

As array in C# is a reference type, the new keyword is used to create an array instance:

string[] strArr = new string[5];

Assigning values at the time of declaration:

string[] strArrCities = new string[] { “New York”, “London”, “Berlin”, “Paris”, “Dubai” };

You may also assign values as follows:

string[] strArr = new string[5];

strArr[0] = "New York";

strArr[1] = "London";

strArr[2] = "Berlin";

strArr[3] = "Paris";

strArr[4] = "Dubai";

The examples below shows declaring, initializing and accessing string arrays in different ways.

An example of a string array

In this example, an array is declared and initialized without specifying the array size. The values are also assigned at the time of declaration. Then, a foreach loop is used to iterate through the array elements and displaying values:

using System;



class string_array_demo

{

    static void Main()

    {

        //Declaring and initializing a string array

        string[] strArrFruit = new string[] { "Mango", "Apple", "Strawberry", "Cherry", "Pine Apple" };



        Console.WriteLine("****The Fruit Names in Strig Array:****\n");



        foreach (string fru in strArrFruit)

        {

            Console.WriteLine("\t" + fru);

        }



        Console.ReadLine();

    }



}

c# string-array

Declaring a string array with size example

In this string array example, we have declared an array of five size. After declaration, the values are assigned by using the index number and then foreach loop is used to display array elements:

using System;



class string_array_demo

{

    static void Main()

    {

        //Declaring and initializing a string array

        string[] ArrAnimals = new string[5];

        ArrAnimals[0] = "Lion";

        ArrAnimals[1] = "Monkey";

        ArrAnimals[2] = "Donkey";

        ArrAnimals[3] = "Bear";

        ArrAnimals[4] = "Wolf";





        Console.WriteLine("****Animals in Strig Array:****\n");



        foreach (string ani in ArrAnimals)

        {

            Console.WriteLine("\t" + ani);

        }



        Console.ReadLine();

    }



}

c# string-array-animals

Using for loop to iterate through a string array

The example below shows using a for loop to iterate through the array elements and displaying on the screen. For the condition part of the for loop, I used the length property of Array as follows:

using System;



class array_for_loop

{

    static void Main()

    {

       

        string[] Arrstr = new string[5];

        Arrstr[0] = "abc";

        Arrstr[1] = "def";

        Arrstr[2] = "jhi";

        Arrstr[3] = "jkl";

        Arrstr[4] = "xyz";



        for (int l = 0; l < Arrstr.Length; l++)

        {



            Console.WriteLine(Arrstr[l]);

        }





        Console.ReadLine();

    }



}

How to convert a string array to string

One of the ways for converting an array to string is using the join method. The join is the method of string class where you may specify the array as shown in the example below:

using System;



class array_to_string

{

    static void Main()

    {

       

        string[] Arrstr = new string[5];

        Arrstr[0] = "This";

        Arrstr[1] = "is";

        Arrstr[2] = "String";

        Arrstr[3] = "Array";

        Arrstr[4] = "Tutorial";



        Console.WriteLine(string.Join(" ", Arrstr));



        Console.ReadLine();

    }



}

The output of the above code is:

This is String Array Tutorial

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!