adspace


How does foreach loop work in c#?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is an abstract class c#?

969


Which namespaces are necessary to create a localized application?

1142


What is expression tree in c#?

998


How to assign Null value to Var?

1064


Why can't we use a static class instead of singleton?

954


How do you inherit a class into other class in c#?

995