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 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 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) 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
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5