a foreach loop

image_pdfimage_print

   

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

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example4_12.cs illustrates the use of
  a foreach loop
*/

public class Example4_12
{

  public static void Main()
  {

    int [] myValues = {2, 4, 3, 5, 1};
    foreach (int counter in myValues)
    {
      System.Console.WriteLine("counter = " + counter);
    }

  }

}

           
          


Search an array using foreach

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Search an array using foreach.

using System;

public class Search {
public static void Main() {
int[] nums = new int[10];
int val;
bool found = false;

// give nums some values
for(int i = 0; i < 10; i++) nums[i] = i; val = 5; // use foreach to search nums for key foreach(int x in nums) { if(x == val) { found = true; break; } } if(found) Console.WriteLine("Value found!"); } } [/csharp]

Use foreach on a two-dimensional array

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use foreach on a two-dimensional array.

using System;

public class ForeachDemo2 {
public static void Main() {
int sum = 0;
int[,] nums = new int[3,5];

// give nums some values
for(int i = 0; i < 3; i++) for(int j=0; j < 5; j++) nums[i,j] = (i+1)*(j+1); // 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]

Use break with a foreach

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use break with a foreach.

using System;

public class ForeachDemo1 {
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; if(x == 4) break; // stop the loop when 4 is obtained } Console.WriteLine("Summation of first 5 elements: " + sum); } } [/csharp]

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);
      }
    }
  }
}