How does foreach loop work in c#?



How does foreach loop work in c#?..

Answer / Anas Malik

The 'foreach' loop in C# is used to iterate through each item in a collection. Its syntax is as follows: nn`foreach (type variable in collection) {n // your code heren}n` where 'variable' is the name given to each item in the collection, and 'collection' is the data structure that contains the items, such as an array, List, or Dictionary. For example, to iterate through an array of integers: nn```nint[] numbers = {1, 2, 3, 4};nforeach (int number in numbers) {n Console.WriteLine(number);n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Sharp Interview Questions

Can a struct inherit from an interface in c#?

1 Answers  


What is generic delegates in c#?

1 Answers  


What do you mean by jagged array?

1 Answers  


What are the 3 different types of arrays?

1 Answers  


Is functional interface runnable?

1 Answers  


What is marker interface?

1 Answers   TCS,


How are Windows programs different from normal C-programs?

1 Answers   Tech Mahindra,


What is ControlBox Propertie

1 Answers   MCN Solutions,


What is a lambda expression in c#?

4 Answers  


Explain lock, monitors, and mutex object in threading.

1 Answers  


How do you implement thread synchronization in c#?

1 Answers  


What is method overriding in c#

1 Answers  


Categories