MorkaLork Development

Interesting stuff I've picked up over the years...

Foreach

2009-04-17 07:26:19 | 203 views | csharp class method tutorial foreach collection

Foreach loops through every item in a collection.

Looks like this:


class Program
{
public static void Main(string[] args)
{
//Foreach with string
string[] months = new string[]{"January", "February", "March", "April", "May", "June",
"July", "August", "September", "August", "November", "December"};

Console.WriteLine("String:");
foreach(string s in months)
{
Console.WriteLine(s);
}

//Foreach with int
int[] numbers = new int[]{1, 2, 3, 4, 5};

Console.WriteLine("
Int:");
foreach(int i in numbers)
{
Console.WriteLine(i);
}

Console.Read();
}
}



Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.