Hello, I'm

Vijay Sharma

I'm a Full Stack Web Developer

An enthusiastic person currently shaping the future of software development by designing and developing smooth user interfaces that promote user interaction with information and data.

About Me

Connecting Developers with the Latest C# Updates

C# | Important points
Tips, Tricks, and Best Practices for Developers

Use the "foreach" loop for iterating over collections.
  • The "foreach" loop is a convenient way to iterate over collections, such as arrays or lists. In this example, we have an array of integers called "numbers". We use the "foreach" loop to iterate over the array and print each number to the console. The loop automatically moves to the next item in the collection until all items have been processed. Compared to a traditional "for" loop, the "foreach" loop is more concise and easier to read, especially when working with complex data structures. It also helps to prevent common errors such as off-by-one errors and index out of range errors.

Example -->

Syntax by using for loop

int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
    Console.WriteLine(number);
}

Post a Comment

0 Comments