Sunday, 18 August 2013


Visual Studio .NET 2012

This tutorial will show you how to use foreach with Arrays in C#.


C# has the foreach statement. Using this statement, you can iterate through the elements of an array.

Example:

Single-Dimensional Arrays

int[] numbers = {1, 2, 3, 4, 5, 6, 7};

foreach (int I in numbers)

{

   System.Console.WriteLine(I);

}


Multi-dimensional Arrays

int[,] numbers = new int[3, 2] {{2, 5}, {4, 21}, {43, 56}};

foreach(int I in numbers)

{

   Console.Write("{0} ", I);

}

Output:
2 5 4 21 43 56

Tip: While using multidimensional arrays use the nested for loop.


See Also

Usingforeach with Arrays - DOTNET

Arrays Tutorial - DOTNET

Get Ebooks delivered to your email id

Comments

Subscribe to our channel

Facebook

Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists