Example Program

C# has a very familiar feel and syntax for anyone experienced with C++ and Java. The "using System" is similar to using a namespace in C++. C# introduces an interesting structure of foreach that allows for the programmer to iterate through an array with a conditional. These conditionals can of course be nested if desired.

  1. using System;
  2. public class Ch2Example {
  3. static void Main() {
  4. int[] inlist;
  5. int listlen,
  6. counter,
  7. sum = 0,
  8. average,
  9. result = 0;
  10. intList = new int[99];
  11. listlen = Int32.Parse(console.readLine());
  12. if ((listen > 0) && (listlen < 100)) {
  13. for (counter = 0; counter < listlen; counter++) {
  14. intList[counter] = Int32.Parse(Console.readLine));
  15. sum += intList[counter];
  16. }
  17. average = sum / listlen;
  18. foreach (int num in intList)
  19. if (num > average) result++;
  20. Console.WriteLine("Number of values > average is:" + result);
  21. }
  22. else
  23. Console.WriteLine("Error-input list length is not legal");
  24. }
  25. }