using System;
using System.Reflection;
public class MyClass {
public static void MethodA() {
Console.WriteLine("MyClass.MethodA");
throw new Exception("MethodA exception");
}
}
public class Starter {
public static void Main() {
try {
Type zType = typeof(MyClass);
MethodInfo method = zType.GetMethod("MethodA");
method.Invoke(null, null);
} catch (Exception except) {
Console.WriteLine(except.Message);
Console.WriteLine("original: " + except.InnerException.Message);
}
}
}
Switch based console menu
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
namespace nsSwitch
{
using System;
public class nsSwitch
{
static void Main ()
{
bool done = false;
do
{
clsAnimal dog = new clsAnimal (1);
clsAnimal cat = new clsAnimal (2);
clsAnimal goldfish = new clsAnimal (3);
clsAnimal aardvark = new clsAnimal (4);
Console.WriteLine ("Select one of the following:");
Console.WriteLine (" 1 -- For dogs");
Console.WriteLine (" 2 -- For cats");
Console.WriteLine (" 3 -- For goldfish");
Console.WriteLine (" 4 -- For aardvarks");
Console.Write ("Enter Your selection (0 to exit): ");
string strSelection = Console.ReadLine ();
int iSel;
try
{
iSel = int.Parse(strSelection);
}
catch (FormatException)
{
Console.WriteLine ("
What?
");
continue;
}
Console.WriteLine ("You selected " + iSel);
switch (iSel)
{
case 0:
done = true;
break;
case 1:
Console.WriteLine (dog);
break;
case 2:
Console.WriteLine (cat);
break;
case 3:
Console.WriteLine (goldfish);
break;
case 4:
Console.WriteLine (aardvark);
break;
default:
Console.WriteLine ("You selected an invalid number: {0}
", iSel);
continue;
}
Console.WriteLine ();
} while (!done);
Console.WriteLine ("
Goodbye!");
}
}
class clsAnimal
{
public clsAnimal (int Type)
{
PetType = Type;
}
private int Type;
public int PetType
{
get {return (Type);}
set {Type = value;}
}
public override string ToString()
{
switch (PetType)
{
default:
return ("Unknown pet");
case 1:
return ("Your pet type is a dog");
case 2:
return ("Your pet type is a cat");
case 3:
return ("Your pet type is a goldfish");
case 4:
return ("Your pet type is an aardvark");
}
}
}
}
Switch Values Fall Through
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class ValuesFallThrough
{
static void Main()
{
String myChoice = "NewLeft";
// switch on the string value of myChoice
switch (myChoice)
{
case "NewLeft":
Console.WriteLine(
"The NewLeft members are voting Democratic.");
goto case "Democrat";
case "Democrat":
Console.WriteLine("You voted Democratic.
");
break;
case "CompassionateRepublican": // fall through
case "Republican":
Console.WriteLine("You voted Republican.
");
Console.WriteLine("Don't you feel compassionate?");
break;
case "Progressive":
Console.WriteLine("You voted Progressive.
");
break;
default:
Console.WriteLine("You did not make a valid choice.");
break;
}
Console.WriteLine("Thank you for voting.");
}
}
Switch With Default Values
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class SwitchWithDefaultValues
{
static void Main()
{
const int Democrat = 0;
const int Republican = 1;
const int Progressive = 2;
// hard wire to Republican
int myChoice = 5;
// switch on the value of myChoice
switch (myChoice)
{
case Democrat:
Console.WriteLine("You voted Democratic.
");
break;
case Republican:
Console.WriteLine("You voted Republican.
");
break;
case Progressive:
Console.WriteLine("You voted Progressive.
");
break;
default:
Console.WriteLine("You did not make a valid choice.");
break;
}
Console.WriteLine("Thank you for voting.");
}
}
Switch Values
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class SwitchValues
{
static void Main()
{
const int Democrat = 0;
const int Republican = 1;
const int Progressive = 2;
// hard wire to Republican
int myChoice = Republican;
// switch on the value of myChoice
switch (myChoice)
{
case Democrat:
Console.WriteLine("You voted Democratic.");
break;
case Republican:
Console.WriteLine("You voted Republican.");
break;
case Progressive:
Console.WriteLine("You voted Progressive.");
break;
}
Console.WriteLine("Thank you for voting.");
}
}
Switch statement containing a branch with no statements: causes a 'fall-through' to the next branch
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_7.cs illustrates the use of
the switch statement containing a branch
with no statements: causes a "fall-through"
to the next branch
*/
public class Example4_7
{
public static void Main()
{
int value = 1;
switch (value)
{
case 0:
System.Console.WriteLine("Zero");
break;
case 1:
case 2:
System.Console.WriteLine("One or two");
break;
case 3:
System.Console.WriteLine("Three");
break;
default:
System.Console.WriteLine("Other number");
break;
}
}
}
Illustrates the use of the switch statement to compare string values
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_6.cs illustrates the use of
the switch statement to compare string values
*/
public class Example4_6
{
public static void Main()
{
string planetName = "Saturn"; // sixth planet from the Sun
switch (planetName)
{
case "Mercury":
System.Console.WriteLine(1);
break;
case "Venus":
System.Console.WriteLine(2);
break;
case "Earth":
System.Console.WriteLine(3);
break;
case "Mars":
System.Console.WriteLine(4);
break;
case "Jupiter":
System.Console.WriteLine(5);
break;
case "Saturn":
System.Console.WriteLine(6);
break;
case "Uranus":
System.Console.WriteLine(7);
break;
case "Neptune":
System.Console.WriteLine(8);
break;
case "Pluto":
System.Console.WriteLine(9);
break;
default:
System.Console.WriteLine("Planet unknown");
break;
}
}
}

