Use the foreach loop

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use the foreach loop.

using System;

public class ForeachDemo {
public static void Main() {
int sum = 0;
int[] nums = new int[10];

// give nums some values
for(int i = 0; i < 10; i++) nums[i] = i; // use foreach to display and sum the values foreach(int x in nums) { Console.WriteLine("Value is: " + x); sum += x; } Console.WriteLine("Summation: " + sum); } } [/csharp]

Hashtable and foreach

image_pdfimage_print

   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;
using System.Collections;

namespace Client.Chapter_3___Structs__Enums__Arrays_and_Classes
{
  public class HashTablesChapter_3___Structs__Enums__Arrays_and_Classes
  {
    static void Main(string[] args)
    {
      Hashtable a = new Hashtable(10);
      Hashtable.Synchronized(a);
            
      a.Add(100, "Arrays");
      a.Add(200, "Classes");
      

      foreach(DictionaryEntry d in a)
      {
        Console.WriteLine(d.Value);
      }

      a.Remove(100);
      a.Remove(200);
      a.Clear();

    }
  }
}

           
          


ArrayList foreach

image_pdfimage_print
   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;
using System.Collections;

namespace Client.Chapter_4___Program_Control
{
  public class ForEaches
  {
    static void Main(string[] args)
    {
      ArrayList a = new ArrayList(10);

      foreach (int x in a)
      {
        Console.WriteLine(x);
      }
    }
  }
}

           
          


Update two parameters in for loop

image_pdfimage_print

using System;

class CommaOp1
{
const int StartChar = 33;
const int EndChar = 125;
const int CharactersPerLine = 5;

static public void Main()
{
for (int i = StartChar, j = 1; i <= EndChar; i++, j++) { Console.Write("{0}={1} ", i, (char)i); if (0 == (j % CharactersPerLine)) { Console.WriteLine(""); } } } } [/csharp]

a for loop to display 1 to 5

image_pdfimage_print

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example4_11.cs illustrates the use of
a for loop to display 1 to 5
*/

public class Example4_11
{

public static void Main()
{

for (int counter = 1; counter <= 5; counter++) { System.Console.WriteLine("counter = " + counter); } } } [/csharp]

For with empty condition

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/
 using System;
 public class ForEmptyTester
 {
     public static void Main()
     {
         int counterVariable = 0;  // initialization

         for ( ;; )
         {
             Console.WriteLine(
                 "counter: {0} ", counterVariable++); // increment

             if (counterVariable > 10) // test
                 break;
         }
     }
 }

           
          


For without increase

image_pdfimage_print

/*
Learning C#
by Jesse Liberty

Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class ForWithoutIncrTester
{

public static void Main()
{

for (int counter = 0; counter<10; ) // no increment { Console.WriteLine( "counter: {0} ", counter); // do more work here counter++; // increment counter } } } [/csharp]