Friday, 24 September 2010

How to Program foreach Loop in C#

This program demonstrate the use of foreach loop. you can use this loop with any collection and you can not modify values while looping.

image

Source Code of ForEach loop:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsForEachLoop
    {
        public static void Main()
        {
            try
            {
                string[] Sites = { "Csharptalk.com", "IndiHub.com", "BharatClick.com", "Csharphub.com", "Sharepointbank.com" };

                foreach (string EachSite in Sites)
                {
                    Console.WriteLine("{0}", EachSite);
                }
            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment