- Learn C# in 7 days
- Gaurav Aroraa
- 162字
- 2021-07-08 09:51:29
The foreach loop
This helps iterate an array element or collection. It does the same thing as the for loop, but this is available to iterate through a collection without the facility to add or remove items from collections.
Let's take a look at the following code snippet:
private static void ForEachStatementExample() { WriteLine("foreach loop example"); char[] vowels = {'a', 'e', 'i', 'o', 'u'}; WriteLine("foreach on Array."); foreach (var vowel in vowels) { WriteLine($"{vowel}"); } WriteLine(); var persons = new List<Person> { new Author {Name = "Gaurav Aroraa"}, new Reviewer {Name = "ShivprasadKoirala"}, new TeamMember {Name = "Vikas Tiwari"}, new TeamMember {Name = "Denim Pinto"} }; WriteLine("foreach on collection"); foreach (var person in persons) { WriteLine($"{person.Name}"); }
}
The preceding code is a working example of a foreach statement that prints a person's name. Name is a property in a collection of the Person object. The statement of the foreach block executes repeatedly until the expression person in persons evaluates to false.
推薦閱讀
- 深入理解Bootstrap
- Git Version Control Cookbook
- PyTorch自動駕駛視覺感知算法實戰
- 區塊鏈架構與實現:Cosmos詳解
- aelf區塊鏈應用架構指南
- Python深度學習:基于TensorFlow
- App Inventor創意趣味編程進階
- Solutions Architect's Handbook
- Learning VMware vSphere
- 從0到1:HTML5 Canvas動畫開發
- Learning Kotlin by building Android Applications
- 軟件測試技術
- HTML5 WebSocket權威指南
- HTML5+CSS3+jQuery Mobile+Bootstrap開發APP從入門到精通(視頻教學版)
- Java EE互聯網輕量級框架整合開發:SSM+Redis+Spring微服務(上下冊)