Saturday, November 7, 2009

FOREACH STATEMENT

foreach is one of the new thing in dot net, it will help the programmer in iterating each value in am array or collection.

using System;
using System.Collections.Generic;
using System.Text;

namespace conditional_statement
{
class Program
{

static void Main(string[] args)
{
int[] a ={ 1, 2, 3, 4, 5, 6, 7 };

foreach (int b in a)
{
Console.WriteLine(b);

}
Console.ReadLine();

}
}
}

Explanation:
Here I have used the statement, foreach (int b in a).
int b is the reference variable I am using for referring value in an array a. Where a is the array name which I have used in the above program.
I have used int b because here I am referring an integer array. So the datatype of the local variable depends on what type of array I am referring.

No comments:

Post a Comment