首先是组合case标签。
例子如下:
参考资料:《C#技术揭秘》
例子如下:
1using System;
2
3enum Tenders : int
4{
5 Cash = 1,
6 Visa,
7 MasterCard,
8 AmericanExpress
9};
10
11class Payment
12{
13 public Payment(Tenders tender)
14 {
15 this.Tender = tender;
16 }
17
18 protected Tenders tender;
19 public Tenders Tender
20 {
21 get
22 {
23 return this.tender;
24 }
25 set
26 {
27 this.tender = value;
28 }
29 }
30
31 public void ProcessPayment()
32 {
33 switch((int)(this.tender))
34 {
35 case (int)Tenders.Cash:
36 Console.WriteLine("\nCash - Always good");
37 break;
38
39 case (int)Tenders.Visa:
40 case (int)Tenders.MasterCard:
41 case (int)Tenders.AmericanExpress:
42 Console.WriteLine("\nDisplay Credit card Authorization Dialog.");
43 break;
44
45 default:
46 Console.WriteLine("\nSorry - Invalid tender");
47 break;
48 }
49 }
50}
51
52class Switch
53{
54 public static void DisPlayMenu()
55 {
56 Console.WriteLine("Which form of payment is" + "Being Used:");
57 Console.WriteLine("\t1 = Cash");
58 Console.WriteLine("\t2 = Visa");
59 Console.WriteLine("\t3 = Master Card");
60 Console.WriteLine("\t4 = American Express");
61 Console.WriteLine("===>");
62 }
63
64 public static int GetTender()
65 {
66 int tenderChoice = -1;
67 DisPlayMenu();
68
69 bool validTender = false;
70 while(!validTender)
71 {
72 string str = Console.ReadLine();
73 tenderChoice = Convert.ToInt32(str);
74
75 if(tenderChoice >= 0 && tenderChoice <=4)
76 validTender = true;
77 else
78 Console.WriteLine("Ivalid choice - try again");
79 }
80
81 return tenderChoice;
82 }
83
84 public static void Main()
85 {
86 Tenders tenderChoice = (Tenders)GetTender();
87 Payment payment = new Payment(tenderChoice);
88 payment.ProcessPayment();
89
90 Console.ReadLine();
91 }
92}
2
3enum Tenders : int
4{
5 Cash = 1,
6 Visa,
7 MasterCard,
8 AmericanExpress
9};
10
11class Payment
12{
13 public Payment(Tenders tender)
14 {
15 this.Tender = tender;
16 }
17
18 protected Tenders tender;
19 public Tenders Tender
20 {
21 get
22 {
23 return this.tender;
24 }
25 set
26 {
27 this.tender = value;
28 }
29 }
30
31 public void ProcessPayment()
32 {
33 switch((int)(this.tender))
34 {
35 case (int)Tenders.Cash:
36 Console.WriteLine("\nCash - Always good");
37 break;
38
39 case (int)Tenders.Visa:
40 case (int)Tenders.MasterCard:
41 case (int)Tenders.AmericanExpress:
42 Console.WriteLine("\nDisplay Credit card Authorization Dialog.");
43 break;
44
45 default:
46 Console.WriteLine("\nSorry - Invalid tender");
47 break;
48 }
49 }
50}
51
52class Switch
53{
54 public static void DisPlayMenu()
55 {
56 Console.WriteLine("Which form of payment is" + "Being Used:");
57 Console.WriteLine("\t1 = Cash");
58 Console.WriteLine("\t2 = Visa");
59 Console.WriteLine("\t3 = Master Card");
60 Console.WriteLine("\t4 = American Express");
61 Console.WriteLine("===>");
62 }
63
64 public static int GetTender()
65 {
66 int tenderChoice = -1;
67 DisPlayMenu();
68
69 bool validTender = false;
70 while(!validTender)
71 {
72 string str = Console.ReadLine();
73 tenderChoice = Convert.ToInt32(str);
74
75 if(tenderChoice >= 0 && tenderChoice <=4)
76 validTender = true;
77 else
78 Console.WriteLine("Ivalid choice - try again");
79 }
80
81 return tenderChoice;
82 }
83
84 public static void Main()
85 {
86 Tenders tenderChoice = (Tenders)GetTender();
87 Payment payment = new Payment(tenderChoice);
88 payment.ProcessPayment();
89
90 Console.ReadLine();
91 }
92}
虽然可以组合case标签,但是不能穿透switch语句。
比如39-43行改成:
case (int)Tenders.Cash:
Console.WriteLine("\nCash - Always good");
case (int)Tenders.Visa:
Console.WriteLine("Visa - Accepted");
case (int)Tenders.MasterCard:
Console.WriteLine("\nMasterCard - Accepted");
case (int)Tenders.AmericanExpress:
Console.WriteLine("\nAmerican Express - Accepted");
break;
Console.WriteLine("\nCash - Always good");
case (int)Tenders.Visa:
Console.WriteLine("Visa - Accepted");
case (int)Tenders.MasterCard:
Console.WriteLine("\nMasterCard - Accepted");
case (int)Tenders.AmericanExpress:
Console.WriteLine("\nAmerican Express - Accepted");
break;
编译时就会报错。
但是我确实想让它实现贯穿的效果呢?当然还是有其他弯路可以走的:
第一种方法就是使用if语句替代:
if(Tenders.Cash = Tenders.Visa)
{
Console.WriteLine("\nCash - Always good");
}
Console.WriteLine("Visa - Accepted");
Console.WriteLine("\nMasterCard - Accepted");
Console.WriteLine("\nAmerican Express - Accepted");
{
Console.WriteLine("\nCash - Always good");
}
Console.WriteLine("Visa - Accepted");
Console.WriteLine("\nMasterCard - Accepted");
Console.WriteLine("\nAmerican Express - Accepted");
(当然,这个例子的语义本来就不适合贯穿,仅作演示)
第二种方法是使用我们一直避讳使用的goto语句:
case (int)Tenders.Cash:
Console.WriteLine("\nCash - Always good");
goto case (int)Tenders.Visa;
case (int)Tenders.Visa:
Console.WriteLine("Visa - Accepted");
goto case (int)Tenders.MasterCard;
case (int)Tenders.MasterCard:
Console.WriteLine("\nMasterCard - Accepted");
goto case (int)Tenders.AmericanExpress;
case (int)Tenders.AmericanExpress:
Console.WriteLine("\nAmerican Express - Accepted");
break;
Console.WriteLine("\nCash - Always good");
goto case (int)Tenders.Visa;
case (int)Tenders.Visa:
Console.WriteLine("Visa - Accepted");
goto case (int)Tenders.MasterCard;
case (int)Tenders.MasterCard:
Console.WriteLine("\nMasterCard - Accepted");
goto case (int)Tenders.AmericanExpress;
case (int)Tenders.AmericanExpress:
Console.WriteLine("\nAmerican Express - Accepted");
break;
参考资料:《C#技术揭秘》