Wednesday, December 19, 2018
Home > C# > C# String Array

String array in C#

In C# string array is an array of string data type. i.e. this array will store the string values at its indexes. We can declare string array in C# as

string [] arrayName = new string[size of array];

e.g.

string [] str_Array = new string[10];

str_Array is the name of the array while 10 is the size of this array. Working of the string array in c# is same as of an array of any other data type (int, char etc.).

Initializing the C# String Array

We can initialize the string array in c# in many ways. The one way to initialize the string array is to create a string array and then assign value to each index of the array.

e.g.

     //declaring the array.
     string [] str_Array = new string[7];
     
     //Initializing the array.
     str_Array[0] = “This”;
     str_Array[1] = “is”;
     str_Array[2] = “a”;
     str_Array[3] = “String”;
     str_Array[4] = “Array”;
     str_Array[5] = “in”;
     str_Array[6] = “C#”;

 

Or you can initialize a C# string array as:

//Declaring and initializing the array using the single statement.

String [] str_Array = new String [] {“This”, “is”, “Another”,”way”,”of”,
                                 ”initializing”, ”C#”,”String”,”Array”};

In this way, we do not explicitly define the size of the array. The compiler starts from the first value and placing it on the first index of the array and so on. Thus obtaining the size of an array by itself.

Accessing the values of the array

The values for an array can be accessed using the index of the array. For the above two examples, we can access the elements of the arrays as:

// this will give us the access to the value at the given index.
     str_Array[index];

Note: – index can be any non-negative value starting from 0 and 1 less than the size of the array. Because arrays store values from 0th index.

using System;
namespace ConsoleApp1
{
     class Program
     {
          static void Main(string[] args)
          {
               string[] str_Array = new string[7];
               str_Array[0] = "This";
               str_Array[1] = "is";
               str_Array[2] = "a";
               str_Array[3] = "String";
               str_Array[4] = "Array";
               str_Array[5] = "in";
               str_Array[6] = "C#";
               for(int i=0;i<7; i++)
               {
                    Console.Write(str_Array[i] + " ");
               }
               Console.Write("\n\n");
               for (int i = 0; i < 7; i++)
               {
                    Console.WriteLine(str_Array[i] + " ");
               }
               Console.WriteLine("");
          }
     }
}

c# string array

Obtaining the size/length of the array

We can get the size of the array using .Length function available in c#. This is useful when we don’t know the size of the array or when we declare and initialize the array using a single statement as we did above.

e.g.

int size = str_Array.Lenght;
using System;
namespace ConsoleApp1
{
     class Program
     {
          static void Main(string[] args)
          {
               String[] str_Array = new String[] {"This", "is", "A",
               "way","of", Initializing","C#",
                "String","Array"};
               int size = str_Array.Length;
               Console.Write("Size of Array is :"+size+"\n");
          }
     }
}

size of c# string array

Resizing the array in c#

C# allows us to resize an array. We can achieve this by the use of Resize function in the c#. The syntax for this is

Array.Resize(ref Array_name, new_Size_of_array);
using System;
namespace ConsoleApp1
{
     class Program
     {
          static void Main(string[] args)
          {          
               string[] str_Array = new string[7];
               str_Array[0] = "This";
               str_Array[1] = "is";
               str_Array[2] = "a";
               str_Array[3] = "String";
               str_Array[4] = "Array";
               str_Array[5] = "in";
               str_Array[6] = "C#";
               for (int i = 0; i < 7; i++)
               {
                    Console.Write(str_Array[i] + " ");
               }
               // Resizing the array
               Array.Resize(ref str_Array, 11);
               str_Array[7] = "&";
               str_Array[8] = "We";
               str_Array[9] = "Resized";
               str_Array[10] = "It.";
               Console.Write("\n\n");
               for (int i = 0; i < str_Array.Length; i++)
               {
                    Console.Write(str_Array[i] + " ");
               }
               Console.WriteLine("");
          }
     }
}

resize the string array in c#





We can use foreach loop in string arrays as well.

     foreach (string element in str_Array)
     {
          Console.WriteLine(element);
     }

Converting the String array to List

We can convert a string array in c# to list as well.

using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] str_Array = new String[] {"This", "is", "a","C#","String","Array",
            "converted", "to","List."};
            //converting to list
            List<string> array_list = new List<string>(str_Array);
            foreach (string value in array_list)
            {
                Console.Write(value + " ");
            }
            Console.WriteLine("");
        }
    }
}

string array to list

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: