How To Declare and access array of string in C#

How To Declare and access array of string in C#

Here we will see how to declare an array of string and access it's elements using for loop. Example uses both C#.net and VB.net programs.

C# declare an array of strings.

String Array Example.

Let's see C# program to declare an access array of strings. It declares and retrieve fourth element of array

using System;
namespace Csharp.ArrayOfStrings
{
class Program
{
static void Main(string[] args)
{
string[] arrayOfStrings = new string[] { "C#", "Dotnet",
"Examples","Declare" , "Array", "Of", "Strings" };
int length = arrayOfStrings.Length;
Console.WriteLine("Fourth element of array is {0}",arrayOfStrings[4]);
for (int index = 0; index < length; index++)
{
Console.WriteLine("String at position number {0} is {1}.", index, arrayOfStrings[index]);
}
Console.ReadLine();
}
}
}

VB.net code declare and access array of strings

VB.net Program

VB.net Example declares an array of strings and retrieves fourth element of it.

Namespace Csharp.ArrayOfStrings
Class Program
Private Shared Sub Main(args As String())
Dim arrayOfStrings As String() = 
New String() {"C#", "Dotnet", "Examples", "Declare", "Array", "Of", _ "Strings"} Dim length As Integer = arrayOfStrings.Length Console.WriteLine("Fourth element of array is {0}", arrayOfStrings(4)) For index As Integer = 0 To length - 1 Console.WriteLine("String at position number {0} is {1}.", index, arrayOfStrings(index)) Next Console.ReadLine() End Sub End Class End Namespace
Output
Fourth element of array is Array
String at position number 0 is C#.
String at position number 1 is Dotnet.
String at position number 2 is Examples.
String at position number 3 is Declare.
String at position number 4 is Array.
String at position number 5 is Of.
String at position number 6 is Strings.

Tags:

Linq Convert String Array To Int ArrayC# DateTime ToString()c# arraylistC# foreach loopConvert Array To Comma Separated String C#Replace string in C#Split String In C#[C#] Remove Last Character From StringC# looping through arrayc# stringC# convert array to list

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.