Learn .Net Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

"foreach" loop in C#

Raga Deepthi G.
30/03/2018 0 1

foreach is a looping statement :

  • repeats a group of statements for each element in an array or an object collection. (or)
  • used to iterate through the collection/ an array to get the required information.


  • Easy implementation.
  • Least possibility of errors. [ especially when working with index based values, like in arrays. ]


foreach(<datatype of collection / array> <variablename> <in> <collection/array>)




foreach(string courseName in coursesList)



  • here, coursesList is a string collection
  • string courseName is called as the "loop variable" which represents the element from the collection in each iteration.
  • in is the pre-defined keyword

Working style of foreach loop:

  • data type of loop variable must and should be the same as the data type of the array/collection that has to be referred.
  • At the time of execution, first the foreach loop goes to the array/collection, counts the number of elements & repeats the loop that many times automatically.
  • At each iteration array/collection location value will be copied into loop variable.

Example 1: In the below example, we'll see how to iterate a group of int type array elements using foreach loop.



Example 2: In the below example, we'll see how to iterate a group of values in a collection.

NOTE : in .NET we've different types of collections. For example we took "List" a generic collection



0 Dislike
Follow 5

Please Enter a comment


Karthik G. | 02/04/2018

Nice concept learning. Very useful information. Please post continuously.

1 0

Other Lessons for You

C #-Program Sample Application
//using system namespace using System; //Namespace declaration namespace first{ //Class declaration class helloworld { //Main method public static void Main() { //Statement to print output on console...

C#.Net Interview Question and Answers
1. What is C#? C# (pronounced "C sharp") is a simple, modern, object-oriented, and type-safe programming language. It will immediately be familiar to C and C++ programmers. C#...

Difference between Abstract Class and Interface
This is probably one of the most commonly asked questions in any job interview for .Net. Before we look into the differences between the two lets find out the main features. Abstract Class By definition...

The Common Language Runtime
i. It is important to understand that the CLR is an execution environment for .NET applications and that includes applications written in C#, F#, VB.Net and other languages that target the .Net Framework. ...

Programming Practice Technique
Any Programming Language required an Algorithm. Algorithm - It is the finite set of instructions in which each and every instruction has the meaning, instructions are not ambiguous and all the instructions...

Looking for .Net Training Classes?

The best tutors for .Net Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn .Net Training with the Best Tutors

The best Tutors for .Net Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more