- 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.
推薦閱讀
- Getting Started with React
- ReSharper Essentials
- Clojure for Domain:specific Languages
- Python爬蟲開發(fā)與項(xiàng)目實(shí)戰(zhàn)
- Learning AndEngine
- 人人都是網(wǎng)站分析師:從分析師的視角理解網(wǎng)站和解讀數(shù)據(jù)
- C語言實(shí)驗(yàn)指導(dǎo)及習(xí)題解析
- Python算法詳解
- 用戶體驗(yàn)可視化指南
- Troubleshooting Citrix XenApp?
- Hadoop大數(shù)據(jù)分析技術(shù)
- JQuery風(fēng)暴:完美用戶體驗(yàn)
- Laravel Design Patterns and Best Practices
- Getting Started with hapi.js
- Scratch少兒編程高手的7個(gè)好習(xí)慣