Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Loops and Final Touches Foreach Loops

Karol Pisula
Karol Pisula
2,578 Points

Foreach problem

I made an array called PhoneBook and how to i use foreach in it foreach("what do i write here" in PhoneBook){} how do i use foreach like what do i write here to loop it

The syntax for a foreach loop is the following.

foreach(data_type var_name in collection) {


so in your example, lets say your phonebook array contained integers representing the digits of a phonenumber. ex: {5551055, 1112222, 4443333}

int[] phonebook = {5551000,1112222,4443333};
foreach(int number in phonebook){

This code would print all numbers in the phonebook array to the console.