C# looping through array

C# looping through array

C# looping through array is very basic program. Here we will see both VB.net and c#.net examples. we will cover the examples of for loop and foreach loop. In output you can see iterated array of strings. bird.▓

C# For Loop

[C#] for loop example

→ ♣Here we will use string array and iterate its element using for loop. For loop in c# is very similar to C language and other popular languages. In VB.net syntax is different.

using System;
namespace Csharp.ForLoop.Example
{
class Program
{
static void Main(string[] args)
{
string []arrofString = new string[] {"example","tutorial","program","code","loop"} ;
int length = arrofString.Length;
for(int i=0; i < length;i++ )     
{
Console.WriteLine("Element number {0} is {1}",i, arrofString[i]) ;
}
Console.ReadLine();
}
}
}
Output
Element number 0 is example
Element number 1 is tutorial
Element number 2 is program
Element number 3 is code
Element number 4 is loop
VB.net-Program

VB.net example of for loop

Namespace Csharp.ForLoop.Example
Class Program
Private Shared Sub Main(args As String())
Dim arrofString As String() = New String() {"example", "tutorial", "program", "code", "looop"}
Dim length As Integer = arrofString.Length
For i As Integer = 0 To length - 1
Console.WriteLine("Elment number {0} is {1}", i, arrofString(i))
Next
Console.ReadLine()
End Sub
End Class
End Namespace

C#.net foreach loop example

using System;
namespace Csharp.ForLoop.Example
{
class Program
{
static void Main(string[] args)
{
string []arrofString = new string[] {"bird","die","program","code","looop"} ;
foreach(string element in arrofString)
{
Console.WriteLine("Element is {0}",element) ;
}
Console.ReadLine();
}
}
}
VB.net-Program

(VB.net) foreach loop example

Namespace Csharp.ForLoop.Example
Class Program
Private Shared Sub Main(args As String())
Dim arrofString As String() = New String() {"bird", "die", "program", "code", "looop"}
For Each element As String In arrofString
Console.WriteLine("Element is {0}", element)
Next
Console.ReadLine()
End Sub
End Class
End Namespace
Output
Element is bird
Element is die
Element is program
Element is code
Element is looop

Tags:

c# arraylistLinq Convert String Array To Int ArrayHow To Declare and access array of string in C#One dimensional array example C#.net VB.netC# Char ArrayC# foreach loopC# Convert-List To ArrayC# Arraylist foreachC# ArrayList Search using Linq Extension OfType MethodC# convert array to listConvert Array To Comma Separated String C#

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.