浅析C# switch和case

开发 后端
这里介绍C# switch和case的扩展,大家评价各不相同,其实本人也感觉有点牵强。其中举了一个Swith扩展的应用,今天突然有了新想法,对它改进了一些。

C# switch和case的扩展,大家评价各不相同,其实本人也感觉有点牵强。其中举了一个Swith扩展的应用,今天突然有了新想法,对它改进了一些。所谓“语不惊人死不休”,且看这次的改进如何。

我先把扩展的源代码贴出来,折叠一下,等看完后面的例子和讲解再回来看。

  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. public static SwithCase<TCase, TOther> Case<TCase, TOther>
    (this SwithCase<TCase, TOther> sc, TCase option, TOther other, bool bBreak)
     where TCase : IEquatable
    <TCase> 
  35. {  
  36. return Case(sc, c=>c.Equals(option), other, bBreak);  
  37. }  
  38.  
  39. public static SwithCase<TCase, TOther> Case<TCase, TOther>
    (this SwithCase<TCase, TOther> sc, Predicate<TCase> predict, TOther other) 
    where TCase : IEquatable
    <TCase> 
  40. {  
  41. return Case(sc, predict, other, true);  
  42. }  
  43.  
  44. public static SwithCase<TCase, TOther> Case<TCase, TOther>
    (this SwithCase<TCase, TOther> sc, Predicate<TCase> predict,
     TOther other, bool bBreak) where TCase : IEquatable
    <TCase> 
  45. {  
  46. if (sc == null) return null;  
  47. if (predict(sc.Value))  
  48. {  
  49. sc.Action(other);  
  50. return bBreak ? null : sc;  
  51. }  
  52. else return sc;  
  53. }  
  54. #endregion  
  55.  
  56. Default#region Default  
  57. public static void Default<TCase, TOther>
    (this SwithCase<TCase, TOther> sc, TOther other)  
  58. {  
  59. if (sc == null) return;  
  60. sc.Action(other);  
  61. }  
  62. #endregion  

到现在为止估计大家应该有一个疑问了,原来的C# switch和case中可以使用“break”直接返回,这里是怎么处理的呢?Case还有第三个参数,它用来处理实是否break,为true时break,false时继续下一个Case。个人感觉大多数情况下,符合某个条件后一般不需要继续其它的了,所以重载传入true,即默认break。与C# switch和case是相反的。

【编辑推荐】

  1. C#局部类型介绍
  2. C#固定指针简单介绍
  3. 浅析C# FTP WebRequest对象
  4. C#分部方法的应用场景
  5. 简单介绍VB.NET和C#
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-20 14:45:13

C# Switch语句

2009-08-27 13:50:08

C# StringBu

2009-08-26 09:54:45

C#打印预览C#打印

2009-08-27 16:18:47

C#类C#结构体

2009-08-31 09:20:37

C#事件注册和注销

2009-09-10 16:38:43

C# get set用

2009-08-07 17:25:37

C# SortedLi

2009-08-17 18:34:50

C# ChangeCo

2009-08-14 17:45:52

C# ArrayLis

2010-02-02 17:20:44

C++ switch-

2009-08-25 17:59:49

C#入门

2009-10-09 09:07:40

C#委托和事件

2009-08-12 14:59:09

C#和Java不同点

2009-08-17 18:04:49

C# 枚举

2009-09-07 14:33:02

C# switch语句

2009-08-27 13:30:11

C# interfac

2009-08-20 16:15:19

C# 匿名方法

2009-08-21 17:24:06

C# SingleIn

2009-08-27 11:43:31

C#语法

2009-08-10 17:36:17

C#扩展方法
点赞
收藏

51CTO技术栈公众号