C # Extension Method switch/case group extension

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.