O'Reilly logo

Unity 3 Game Development HOTSHOT by Jate Wittayabundit

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The foreach keyword

C# iterators use foreach instead of for. Also, notice the variable declaration within the for/foreach statement. C# requires the type of the item contained in the list to be explicitly declared.

JavaScript:

for (var item in itemList) {
  item.DoSomething();
}

C#:

foreach (ItemType item in itemList) {
  item.DoSomething();
}

Note

Although the JavaScript version uses inefficient dynamic typing (since you can't declare the type), the static-typed alternative is as follows.

JavaScript:

for (var item = itemList.GetEnumerator(); item.MoveNext();) {
  item.DoSomething();
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required