Tuesday 28 January 2014

Foreach loop in C#

A foreach loop works on each object in a collection, and provides that object as the appropriate type within the loop syntax:
 foreach (string in array) 
 {

 }
Example For an Foreach Loop :-
using System;
namespace foreach
{
  class Program
   {
     static void Main(string[] args)
      {
        string[] arr = new string[4]; // declaring array

        
        arr[0] = "Munesh";
        arr[1] = "jhon";
        arr[2] = "dom";
        arr[3] = "roy";
       

        //retrieving value using foreach loop
        foreach (string name in arr)
         {
           Console.WriteLine("Hi " + name);
         }
        Console.ReadLine();
      }
   }
}

OUTPUT
Hello Munesh
Hello Jhon
Hello Dom
Hello roy

No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...