Recursive Factorial method.

image_pdfimage_print

using System;

public class FactorialTest
{
public static void Main( string[] args )
{
for ( long counter = 0; counter <= 10; counter++ ) Console.WriteLine( "{0}! = {1}", counter, Factorial( counter ) ); } public static long Factorial( long number ) { if ( number <= 1 ) return 1; else return number * Factorial( number - 1 ); } } [/csharp]

Use a recursive method, travel, to journey from start to finish

image_pdfimage_print

using System;

public class Journey {

private static String indent = “”;

public static void TakeOneStep(int step) {
Console.WriteLine(“{0}Taking step {1}”, indent, step);
}

public static void Move(int start, int finish) {
string oldIndent = indent;
Console.WriteLine(“{0}Starting move from {1} to {2}”, indent, start, finish);
if (start < finish) { TakeOneStep(start); indent += " "; Move(start+1, finish); indent = oldIndent; } Console.WriteLine("{0}Finishing move from {1} to {2}",indent, start, finish); } public static void Main(String [] args) { Move(1, 10); } } [/csharp]

Recursive sum method

image_pdfimage_print

public class SumPrices {

public static double Sum(double[] p, int start, int end) {
if (start < end) return p[start] + Sum(p,start+1,end); else return 0; } public static void Main() { double[] prices = {1.3, 13.68, 314.919, 82.827, 363.949}; System.Console.WriteLine("The sum is {0:C}", Sum(prices,0,prices.Length)); } } [/csharp]

Foreach for arraylist

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

           
          


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

           
          


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

           
          


Sums the values in an array using a foreach loop

image_pdfimage_print

/*
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); } } } [/csharp]