Syntax of foreach Loop



foreach (data type loopvariable in Array / list collection name)
{
            Statements
            .
            .
}


Working nature of For Each loop        
  • In for each loop Data Type of loop  variable must and should be same as data type of the Array / Collection that we refer
  • At the time of execution 1st foreach loop goes to the array counts number of elements in the Array and repeats the loop that many times automatically
  • At each iteration array is location value will be copied into loop variable L

 Example:-
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Program to print Array Elements using For Each Loop
namespace CABasics
{
    class ClsArray3
    {
        static void Main()
        {
            string[] A = new string[5] {"sampath","srinivas","kalpana","padma","lalitha"};
            Console.WriteLine("Elements of array are");
            foreach (string L in A)
            {
                Console.Write(L + " ");
            }
            Console.Read();

        }
    }
}



Output:-


Comments in :-
                         C#.Net        //Single line comment
                                         /*Multiline Comment*/
                      VB.Net     ‘single line comment.
                       Does not support multi line Comment

No comments:

Post a Comment