/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example4_5.cs illustrates the use of the switch statement */ public class Example4_5 { public static void Main() { int planetPosition = 4; // Mars switch (planetPosition) { case 1: System.Console.WriteLine("Mercury"); break; case 2: System.Console.WriteLine("Venus"); break; case 3: System.Console.WriteLine("Earth"); break; case 4: System.Console.WriteLine("Mars"); break; case 5: System.Console.WriteLine("Jupiter"); break; case 6: System.Console.WriteLine("Saturn"); break; case 7: System.Console.WriteLine("Uranus"); break; case 8: System.Console.WriteLine("Neptune"); break; case 9: System.Console.WriteLine("Pluto"); break; default: System.Console.WriteLine("Planet unknown"); break; } } }
Empty cases can fall through
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Empty cases can fall through.
using System;
public class EmptyCasesCanFall {
public static void Main() {
int i;
for(i=1; i < 5; i++) switch(i) { case 1: case 2: case 3: Console.WriteLine("i is 1, 2 or 3"); break; case 4: Console.WriteLine("i is 4"); break; } } } [/csharp]
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: '{0}'", sb); Console.WriteLine("The length is: {0}", length); sb.Length = 4; length = sb.Length; Console.WriteLine("The result is: '{0}'", sb); Console.WriteLine("The length is: {0}", length); sb.Length = 20; length = sb.Length; Console.WriteLine("The result is: '{0}'", sb); Console.WriteLine("The length is: {0}", length); } }