Tutorialspoint.dev

C# | Arrays of Strings

An array is a collection of the same type variable. Whereas a string is a sequence of Unicode characters or array of characters. Therefore arrays of strings is an array of arrays of characters. Here, string array and arrays of strings both are same term.

For Example, if you want to store the name of students of a class then you can use the arrays of strings. Arrays of strings can be one dimensional or multidimensional.

Declaring the string array: There are two ways to declare the arrays of strings as follows

  • Declaration without size:

    Syntax:

    String[] variable_name;



    or

    string[] variable_name;

  • Declaration with size:

    Syntax:

    String[] variable_name = new String[provide_size_here];

    or

    string[] variable_name = new string[provide_size_here];

Example:

// declaration using string keyword
string[] s1;

// declaration using String class object
// by giving its size 4
String[] s2 = new String[4];

Initialization of Arrays of Strings: Arrays can be initialized after the declaration. It is not necessary to declare and initialize at the same time using the new keyword. However, Initializing an Array after the declaration, it must be initialized with the new keyword. It can’t be initialized by only assigning values.



Example:

// Declaration of the array
string[] str1, str2;

// Intialization of array
str1 = new string[5]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” };

str2 = new string[]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” };

Note: Initialization without giving size is not valid in C#. It will give compile time error.

Example: Wrong Declaration for initializing an array

// compile-time error: must give size of an array
String[] str = new String[];

// error : wrong initialization of an array
string[] str1;
str1 = {“Element 1”, “Element 2”, “Element 3”, “Element 4” };

Accessing Arrays of Strings Elements: At the time of initialization, we can assign the value. But, we can also assign the value of array using its index randomly after the declaration and initialization. We can access an array value through indexing, placed index of the element within square brackets with the array name.

Example:



// declares & initializes string array
String[] s1 = new String[2];

// assign the value "Geeks" in array on its index 0
s1[0] = 10; 

// assign the value "GFG" in array on its index 1
s1[1] = 30;

// assign the value "Noida" in array on its index 2
s1[2] = 20;


// Accesing array elements using index
s1[0];  // returns Geeks
s1[2];  // returns Noida

Declaration and initialization of string array in a single line: String array can also be declared and initialized in a single line. This method is more recommended as it reduces the line of code.

Example:

String[] weekDays = new string[3] {"Sun", "Mon", "Tue", "Wed"}; 

Code#1: String array declaration, initialization and accessing its elements

// C# program to illustrate the String array 
// declaration, initialization and accessing 
// its elements
using System;
  
class Geeks {
      
    // Main Method
    public static void Main()
    {
        // Step 1: Array Declaration
        string[] stringarr; 
          
        // Step 2:Array Initialization
        stringarr = new string[3] {"Element 1", "Element 2", "Element 3"}; 
          
        // Step 3:Accessing Array Elements
        Console.WriteLine(stringarr[0]); 
        Console.WriteLine(stringarr[1]); 
        Console.WriteLine(stringarr[2]); 
    }
}

Output:

Element 1
Element 2
Element 3

Code#2: Array declaration and initialization in single line

// C# code to illustrate Array declaration
// and initialization in single line
using System;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
        // array initialization and declaration
        String[] stringarr = new String[] {"Geeks", "GFG", "Noida"}; 
  
        // accesing array elements
        Console.WriteLine(stringarr[0]);
        Console.WriteLine(stringarr[1]);
        Console.WriteLine(stringarr[2]);
    }
}

Output:

Geeks
GFG
Noida

Note:

  • In the public static void main(String[] args), String[] args is also an array of string.

    Example: To show String[] args is an array of string.

    // C# program to get the type of "args"
    using System;
      
    class GFG {
      
        // Main Method
        static public void Main (String[] args) {
              
            // using GetType() method to
            // get type at runtime
            Console.WriteLine(args.GetType());
        }
    }

    Output:

    System.String[]
    
  • C# string array is basically an array of objects.
  • It doesn’t matter whether you are creating an array of string using string keyword or String class object. Both are same.

    Example:

    // C# program to get the type of arrays of 
    // strings which are declared using 'string'
    // keyword and 'String class object'
    using System;
      
    class GFG {
      
        // Main Method
        static public void Main (String[] args) {
      
            // declaring array of string 
            // using string keyword
            string[] s1 = {"GFG", "Noida"};
      
             // declaring array of string 
            // using String class object
            String[] s2 = new String[2]{"Geeks", "C#"};
      
            // using GetType() method to
            // get type at runtime
            Console.WriteLine(s1.GetType());
            Console.WriteLine(s2.GetType());
        }
    }

    Output:

    System.String[]
    System.String[]
    


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter