close
C# Online.NET Visual C# Developer Center
Search

C# FAQ: What is the difference between C# and Java foreach loops

What is the difference between C# and Java foreach loops?

The foreach loop is a simple syntax for iterating through arrays or collections—implementing the Java: java.lang.Iterable interface or the C#: System.Collections.IEnumerable interface.

Whereas in the Java language, the for keyword and the : operator are used to create a foreach loop, the C# foreach and in keywords are used for this purpose.

// C# example
string[] oddBalls = {"one", "three", "five", "seven"};
 
foreach (string currentBall in oddBalls)
{
   Console.WriteLine (currentBall + " is an odd number.");
}
// Java example
String[] oddBalls = {"one", "three", "five", "seven"};
 
for (String currentBall : oddBalls)
{
   System.out.println (currentBall + " is an odd number.");
}

In C++, the syntax is for_each in <algorithm>.


More Visual C# and Java Language FAQs

Go to Visual CSharp FAQ (main page).

Consider these related Frequently Asked Questions—FAQs:

  1. Are C# classes allocated on the heap?
  2. Are C# strings immutable?
  3. Does C# have a throws clause?
  4. Does C# support boxing?
  5. Does C# support final classes?
  6. Does C# support jagged arrays?
  7. Does C# support static initialization blocks?
  8. How do Java and C# objects compare?
  9. What are the differences between C# and Java access modifiers?
  10. What are the differences between C# and Java constant declarations?
  11. What are the differences between C# and Java constructors?
  12. What are the differences between C# and Java exceptions?
  13. What are the differences between C# and Java garbage collection?
  14. What are the differences between C# and Java generics?
  15. What are the differences between C# and Java inheritance syntax?
  16. What are the differences between C# and Java keywords?
  17. What are the differences between C# and Java Main methods?
  18. What are the differences between C# and Java member initialization?
  19. What are the differences between C# and Java primitive types?
  20. What are the differences between C# and Java reflection?
  21. What are the differences between C# and Java runtimes?
  22. What are the differences between C# and Java variable length parameter lists?
  23. What are the differences between C# and Java virtual machines?
  24. What are the differences between C# finalizers and Java destructors?
  25. What is the difference between C# and Java array declarations?
  26. What is the difference between C# and Java constructor chaining?
  27. What is the difference between C# and Java enumerations (enum)?
  28. What is the difference between C# and Java foreach loops?
  29. What is the difference between C# and Java threads?
  30. What is the difference between C# lock and Java synchronized?
  31. What is the difference between C# using and Java package?
  32. What is the difference between calling base class constructors in C# and Java?
  33. What is the difference between metadata annotations in C# and Java?
  34. What is the difference between nested classes in C# and Java?