Csharp/C Sharp/Language Basics/Foreach

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

a foreach loop

<source lang="csharp"> /* 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);
   }
 }

}

      </source>


ArrayList foreach

<source lang="csharp"> /*

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

}

      </source>


Foreach for arraylist

<source lang="csharp"> /*

* 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 MyArrayList
 {
   static void Main(string[] args)
   {
     ArrayList a = new ArrayList(10);
     ArrayList.Synchronized(a);
     int x = 0;
     a.Add(x);
     a.Insert(1, ++x);
     foreach (int y in a)
     {
       Console.WriteLine(y);
     }
     a.Remove(x);
     a.RemoveAt(0);
     a.Clear();
   }
 }

}

      </source>


Hashtable and foreach

<source lang="csharp"> /*

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

}

      </source>


Queue enqueue and foreach

<source lang="csharp"> /*

* 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 QueuesChapter_3___Structs__Enums__Arrays_and_Classes
 {
   static void Main(string[] args)
   {
     Queue a = new Queue(10);
     int x = 0;
     a.Enqueue(x);
     x++;
     a.Enqueue(x);
     foreach (int y in a)
     {
       Console.WriteLine(y);
     }
     a.Dequeue();
     a.Clear();
   }
 }

}

      </source>


Search an array using foreach

<source lang="csharp"> /* 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!"); 
 } 

}

      </source>


Stack push and foreach

<source lang="csharp"> /*

* 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 StacksChapter_3___Structs__Enums__Arrays_and_Classes
 {
   static void Main(string[] args)
   {
     Stack a = new Stack(10);
     int x = 0;
     a.Push(x);
     x++;
     a.Push(x);
     foreach (int y in a)
     {
       Console.WriteLine(y);
     }
     a.Pop();
     a.Clear();
   }
 }

}

      </source>


Sums the values in an array using a foreach loop

<source lang="csharp"> /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794

  • /

// ForEach.cs -- Sums the values in an array using a foreach loop // // Compile this program with the folowing command line // C:>csc ForEach.cs // namespace nsForEach {

   using System;
   public class ForEach
   {
       static public void Main ()
       {
            DateTime now = DateTime.Now;
            Random rand = new Random ((int) now.Millisecond);
            int [] Arr = new int [10];
            for (int x = 0; x < Arr.Length; ++x)
            {
                Arr[x] = rand.Next () % 100;
            }
            int Total = 0;
            Console.Write ("Array values are ");
            foreach (int val in Arr)
            {
                Total += val;
                Console.Write (val + ", ");
            } 
            Console.WriteLine ("and the average is {0,0:F1}",
                              (double) Total / (double) Arr.Length);
       }
   }

}


      </source>


Use break with a foreach

<source lang="csharp"> /* 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); 
 } 

}

      </source>


Use foreach on a two-dimensional array

<source lang="csharp"> /* 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); 
 } 

}

      </source>


Use the foreach loop

<source lang="csharp"> /* 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); 
 } 

}

      </source>