Implementing C# String Array

An array of strings in C# is a commonly used data type. In this article, you will learn how to implement an array of strings in C#.
 
String array CSharp 
 
There are there common ways to implement string arrays in C#: 
  1. Using an array of strings 
  2. Array class
  3. ArrayList class. 

1. Array of strings

 
The simplest form of an array of strings is using string syntaxes on a string type. The following code snippet creates an array of strings. 
  1. string[] strArray = new string[] { "Mahesh Chand""Mike Gold""Raj Beniwal""Praveen Kumar""Dinesh Beniwal" };  
We can access an array item by passing the item index in the array. The following code snippet copies the first item of the array to a string variable. 
  1. string name = strArray[0];  
The following code is an example of creating an array of string and reading one item at a time. 
  1. using System;  
  2. namespace ArraysSample  
  3. {  
  4. class Program  
  5. {  
  6. static void Main(string[] args)  
  7. {  
  8. Console.WriteLine("Arrays Sample!");  
  9. // Construct a dynamic array and initialize with 5 strings  
  10. string[] strArray = new string[] { "Mahesh Chand""Mike Gold""Raj Beniwal""Praveen Kumar""Dinesh Beniwal" };  
  11. // Display array items  
  12. Console.WriteLine(strArray[0]);  
  13. Console.WriteLine(strArray[1]);  
  14. Console.WriteLine(strArray[2]);  
  15. Console.WriteLine(strArray[3]);  
  16. Console.WriteLine(strArray[4]);  
  17. Console.ReadKey();  
  18. }  
  19. }  
  20. }  
The foreach control statement is used to iterate through the items of an array. The following code snippet uses a foreach loop to read all items of an array. 
  1. string[] strArray = new string[] { "Mahesh Chand""Mike Gold""Raj Beniwal""Praveen Kumar""Dinesh Beniwal" };  
  2. foreach (string str in strArray)  
  3. Console.WriteLine(str);  

2. Array of strings using Array class

 
Array class in .NET provides functionality to work with arrays including construction, manipulation, search, and sort.
 
Array class provides the CreateInstance method to construct an array. The CreateInstance method takes first parameter as the type of item and second and third parameters are the dimension and their range.
 
The following code snippet constructs an Array of 4 items of string type. 
  1. Array authorArray = Array.CreateInstance(typeof(String), 4);  
Once an array is created, use SetValue method to add items to an array. The SetValue method takes the first parameter as the value of the item, followed by the position of the item.
 
The following code snippet adds four items to the array. 
  1. authorArray.SetValue("Mahesh Chand", 0);  
  2. authorArray.SetValue("Raj Kumar", 1);  
  3. authorArray.SetValue("Neel Beniwal", 2);  
  4. authorArray.SetValue("David McCarter", 3);  
The GetValue method is used to read the value of an item. The following code snippet reads the first item of an array. 
  1. string author1 = authorArray.GetValue(0).ToString();  
  2. Console.WriteLine(author1);  
The following code snippet loops through all items of an array. 
  1. foreach (string author in authorArray)  
  2. {  
  3. Console.WriteLine(author);  
  4. }  

3. Array of strings using ArrayList class

 
The ArrayList class is a collection class that provides built in functionality to work with a collection of items in an array including strings.
The following code example creates an ArrayList object and adds strings to it using the Add method. 
  1. ArrayList authorsArray = new ArrayList();  
  2. authorsArray.Add("Mahesh Chand");  
  3. authorsArray.Add("Praveen Kumar");  
  4. authorsArray.Add("Raj Kumar");  
  5. authorsArray.Add("Dinesh Beniwal");  
The Insert method of the ArrayList class is used to insert an object at a given position. The following code example inserts a string at the 3rd position and a Queue at the 2nd position within an ArrayList. 
  1. authorsArray.Insert(3, "Sam Hobbs");  
The ArrayList is a collection and we can use the foreach loop to loop through its items one at a time. The following code example reads all items in an ArrayList and displays on the console. 
  1. foreach (object item in authorsArray)  
  2. {  
  3. Console.WriteLine(item);  
  4. }  
To retrieve an item, we can use the array index within an ArrayList. The following code example reads the second item of an ArrayList. 
  1. Console.WriteLine(authorsArray[2]);  

Summary

 
This article demonstrated how to implement string arrays in C#.
 
Want more arrays? Here is a detailed tutorial on arrays: Working with Arrays in C# 


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.