HOW TO IMPLEMENT FOREACH LOOP IN C# PROGRAMMING?

Jonas Stuart

Total Post:27

Points:189
Posted by  Jonas Stuart
C# 
C#
 844  View(s)
Ratings:
Rate this:
HI Guys

I am beginner in c# programming language .I  want  to know that forEach loop in c# programming.I would really appreciate your help.

Thanks
  1. Elena Glibart

    Post:42

    Points:296
    Re: How to implement for each loop in c# programming?


    The difference between foreach with other looping operations is that foreach is dynamic in nature, whereas other loops are not dynamic.  We use foreach concept when we don't aware about the number of entries in the program.


    Below is a basic program to learn how to use foreach concept in C# language. Although we can implement below program by using For or While loop, but this program is helpful to understand how to use foreach concept.


    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {

        class Program

        {

            static void Main(string[] args)

            {

                int n = 0;

     

                Console.WriteLine("Please enter the number of entry");

     

                n = Convert.ToInt32(Console.ReadLine());

     

                string[] str = new string[n]; // declaring array

     

                for (int i = 0; i < n; i++)

                {

                    Console.WriteLine("Please the entry number {0}", i);

                    str[i] = Console.ReadLine();

                }

     

     

                foreach (string str1 in str)

                {

                    Console.WriteLine("entry :  " + str1);

                }

                Console.ReadLine();

            }

        }

    }

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!