c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

How to perform basic foreach loop?

| | CSharp

This example describes how to perform foreach statement. The foreach statement iterates through a collection that implements the IEnumerable interface. When we compare with for statement foreach does’t use the indexes.

   static void Main(string[] args)
       {
            try
            {
                var names = new List<string>() { "Car", "Bike", "Bus" };
                foreach (string name in names)
                {
                    Console.WriteLine(name);
                }
            }
           catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
       }

 

Output:

 

Car

Bike

Bus