CopyPastehas never been so tasty!

String arrays in C#

by anonymous

  • 0
  • 0
  • 0
96 views

String arrays in C#

String ArraysString Arrays are used to store more than one string. We create and declare string arrays like that: 

string[] arr1 = new string[] { "one", "two", "three" }; 

We can also perform many operations on string array like we can sort them as well as can modify them to make it possible that string arrays can store a range of items instead of just one.We can modify current array as follow: 

arr1[1] = “five”;

arr1[2] = “six”; 

The following code will demonstrate that how we can initialize and fill string array. Furthermore, we can modify them as well.Code

 using System;

using System.Collections;

class Program

    {

        static void Main(string[] args)

        {

            string[] mystring = { "This", "is", "my", "pen" };

 

            Console.WriteLine("Original array: ");

            for (int i = 0; i < mystring.Length; i++)

                Console.Write(mystring[i] + " ");

                Console.WriteLine("\n");

             // for modification

            mystring[2] = "also";

            mystring[3] = "my pen";

             Console.WriteLine("Modified array: ");

            for (int i = 0; i < mystring.Length; i++)

                Console.Write(mystring[i] + " ");

            Console.ReadLine();

         }

    }

Add A Comment: