Join Bruce Van Horn for an in-depth discussion in this video The foreach loop, part of C# Essential Training: 2 Flow Control, Arrays, and Exception Handling.
- [Narrator] After that brief diversion,…we now return you to your regularly scheduled program.…We've seen the ForLoop.…Let's try a ForEachLoop.…So, ForEachLoop in actually used heavily…in the context of working with arrays and lists,…so I'll do almost the same kind of loop.…This will be a ForEachLoop though…and as I said, we'd like to use these with lists,…so let's create a new list and I'll call it numbers…equals new list and it will be of type integer…and just to keep things fresh,…I'm going to show you a slightly different way…to initialize your list before we would just hit…our semicolon here and then, we would say numbers.ad,…but let me show you something a little bit different.…
I can actually use what's called the Collection Initializer…here and I can do this in one step, so let's add…in our numbers one, three, five, seven and nine.…Since our objective here is to add up all the numbers…in the list, I'll create a variable…called sum and set it equal to zero.…Now for the ForEachLoop.…I'll just say foreach(var number in numbers);…
AuthorBruce Van Horn
- Writing unit tests in C#
- Working with simple and multidimensional arrays
- Managing ordered and unordered data with lists
- Evaluating conditions with if-else statements
- Using OR, AND, and NOT operators
- Building loops
- Debugging and handling exceptions
- Creating the final build of your C# project
Skill Level Beginner
C# Object-Oriented Programming Tips and Trickswith Jesse Freeman58m 39s Intermediate
1. Unit Testing and Test-Driven Development
2. Arrays and Collections
3. Flow Control
4. Exception Handling
5. Getting to Production
Next steps1m 57s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.