Use a char to control the switch

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use a char to control the switch.

using System;

public class SwitchDemo2 {
public static void Main() {
char ch;

for(ch='A'; ch<= 'E'; ch++) switch(ch) { case 'A': Console.WriteLine("ch is A"); break; case 'B': Console.WriteLine("ch is B"); break; case 'C': Console.WriteLine("ch is C"); break; case 'D': Console.WriteLine("ch is D"); break; case 'E': Console.WriteLine("ch is E"); break; } } } [/csharp]

Demonstrate the switch

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the switch.

using System;

public class SwitchDemo {
public static void Main() {
int i;

for(i=0; i<10; i++) switch(i) { case 0: Console.WriteLine("i is zero"); break; case 1: Console.WriteLine("i is one"); break; case 2: Console.WriteLine("i is two"); break; case 3: Console.WriteLine("i is three"); break; case 4: Console.WriteLine("i is four"); break; default: Console.WriteLine("i is five or more"); break; } } } [/csharp]

Simulate a conveyor belt


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Simulate a conveyor belt 
 
using System; 
 
class ConveyorControl { 
  // enumerate the conveyor commands 
  public enum action { start, stop, forward, reverse }; 
 
  public void conveyor(action com) { 
    switch(com) { 
      case action.start: 
        Console.WriteLine("Starting conveyor."); 
        break; 
      case action.stop: 
        Console.WriteLine("Stopping conveyor."); 
        break; 
      case action.forward: 
        Console.WriteLine("Moving forward."); 
        break; 
      case action.reverse: 
        Console.WriteLine("Moving backward."); 
        break; 
    } 
  } 
} 
 
public class ConveyorDemo { 
  public static void Main() { 
    ConveyorControl c = new ConveyorControl(); 
 
    c.conveyor(ConveyorControl.action.start); 
    c.conveyor(ConveyorControl.action.forward); 
    c.conveyor(ConveyorControl.action.reverse); 
    c.conveyor(ConveyorControl.action.stop); 
     
  } 
}

           
          


Switch for int type

   

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

namespace Client.Chapter_4___Program_Control
{
  public class MyMainClass1
  {
    static void Main(string[] args)
    {
      int a = 0;
      Console.ReadLine();

      switch (a)
      {
        case 1:
          Console.WriteLine("One");
          break;
        case 2:
          Console.WriteLine("Two");
          break;
        default:
          Console.WriteLine("?");
          break;

      }
    }
  }
}

           
          


StringBuilder: write line


   


using System;
using System.Text;

public class SampleBuilder
{

    public static void Main()
    {

        StringBuilder sb = new StringBuilder("test string");
        int length = 0;
    
        length = sb.Length;
        Console.WriteLine("The result is: &#039;{0}&#039;", sb);
        Console.WriteLine("The length is: {0}", length);
    
        sb.Length = 4;
        length = sb.Length;
        Console.WriteLine("The result is: &#039;{0}&#039;", sb);
        Console.WriteLine("The length is: {0}", length);
    
        sb.Length = 20;
        length = sb.Length;
        Console.WriteLine("The result is: &#039;{0}&#039;", sb);
        Console.WriteLine("The length is: {0}", length);

    }
}

           
          


StringBuilder foreach


   


using System;
using System.Text;

public class StringBuilderDemo {
    public static void Main()
    {
        string s = "I will not buy this record, it is scratched";
        char[] separators = new char[] {&#039; &#039;, &#039;,&#039;};
        StringBuilder sb = new StringBuilder();
        int number = 1;
        
        foreach (string sub in s.Split(separators))
        {
            sb.AppendFormat("{0}: {1} ", number++, sub);
        }
        Console.WriteLine("{0}", sb);
    }
}
           
          


StringBuffer: Replacing Characters


   

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

namespace Client.Chapter_6___Strings
{
  public class ReplacingCharacters
  {
    static void Main(string[] args)
    {
      StringBuilder MyString = new StringBuilder("AAAAABBB");

      Console.WriteLine(MyString);
      MyString.Replace("A", "F");
      Console.WriteLine(MyString);
    }
  }
}