The second section of metamorphosis provides extensions for if/else, swith/case, and while. The comments vary and I feel a little far-fetched. One of the examples is a Swith extended application. Today, we suddenly have a new idea and made some improvements to it. The so-called "language is not surprisingly endless", and it depends on how this improvement is made.
1 public static class SwithCaseExtension
2 {
3 SwithCase # region SwithCase
4 public class SwithCase <TCase, TOther>
5 {
6 public SwithCase (TCase value, Action <TOther> action)
7 {
8 Value = value;
9 Action = action;
10}
11 public TCase Value {get; private set ;}
12 public Action <TOther> Action {get; private set ;}
13}
14 # endregion
15
16 Swith # region Swith
17 public static SwithCase <TCase, TOther> Switch <TCase, TOther> (this TCase t, Action <TOther> action) where TCase: IEquatable <TCase>
18 {
19 return new SwithCase <TCase, TOther> (t, action );
20}
21
22 public static SwithCase <TCase, TOther> Switch <TInput, TCase, TOther> (this TInput t, Func <TInput, TCase> selector, Action <TOther> action) where TCase: IEquatable <TCase>
23 {
24 return new SwithCase <TCase, TOther> (selector (t), action );
25}
26 # endregion
27
28 Case # region Case
29 public static SwithCase <TCase, TOther> Case <TCase, TOther> (this SwithCase <TCase, TOther> SC, TCase option, TOther other) where TCase: IEquatable <TCase>
30 {
31 return Case (SC, option, other, true );
32}
33
34
35 public static SwithCase <TCase, TOther> Case <TCase, TOther> (this SwithCase <TCase, TOther> SC, TCase option, TOther other, bool bBreak) where TCase: IEquatable <TCase>
36 {
37 return Case (SC, c => c. Equals (option), other, bBreak );
38}
39
40
41 public static SwithCase <TCase, TOther> Case <TCase, TOther> (this SwithCase <TCase, TOther> SC, Predicate <TCase> predict, TOther other) where TCase: IEquatable <TCase>
42 {
43 return Case (SC, predict, other, true );
44}
45
46 public static SwithCase <TCase, TOther> Case <TCase, TOther> (this SwithCase <TCase, TOther> SC, Predicate <TCase> predict, TOther, bool bBreak) where TCase: IEquatable <TCase>
47 {
48 if (SC = null) return null;
49 if (predict (SC. Value ))
50 {
51 SC. Action (other );
52 return bBreak? Null: SC;
53}
54 else return SC;
55}
56 # endregion
57
58 Default # region Default
59 public static void Default <TCase, TOther> (this SwithCase <TCase, TOther> SC, TOther other)
60 {
61 if (SC = null) return;
62 SC. Action (other );
63}
64 # endregion
65}
This Code defines three extensions: Switch, Case, and Default.
First, let's look at one of the simplest applications of these extensions, as shown below:
1 string typeName = string. Empty;
2 typeId. Switch (string s) => typeName = s)
3