189 8069 5689

浅析C#switch和case

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

成都创新互联公司是一家集网站建设,崂山企业网站建设,崂山品牌网站建设,网站定制,崂山网站建设报价,网络营销,网络优化,崂山网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

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

 
 
 
  1. public static class SwithCaseExtension  
  2. {  
  3. SwithCase#region SwithCase  
  4. public class SwithCase, TOther> 
  5. {  
  6. public SwithCase(TCase value, Action action)  
  7. {  
  8. Value = value;  
  9. Action = action;  
  10. }  
  11. public TCase Value { get; private set; }  
  12. public Action Action { get; private set; }  
  13. }  
  14. #endregion  
  15.  
  16. Swith#region Swith  
  17. public static SwithCase, TOther> Switch, TOther>
    (this TCase t, Action action) where TCase : IEquatable 
  18. {  
  19. return new SwithCase, TOther>(t, action);  
  20. }  
  21.  
  22. public static SwithCase, TOther> Switch, TCase, TOther>
    (this TInput t, Func, TCase> selector, Action action)
     where TCase : IEquatable 
  23. {  
  24. return new SwithCase, TOther>(selector(t), action);  
  25. }  
  26. #endregion  
  27.  
  28. Case#region Case  
  29. public static SwithCase, TOther> Case, TOther>
    (this SwithCase, TOther> sc, TCase option, TOther other) 
    where TCase : IEquatable 
  30. {  
  31. return Case(sc, option, other, true);  
  32. }  
  33.  
  34. public static SwithCase, TOther> Case, TOther>
    (this SwithCase, TOther> sc, TCase option, TOther other, bool bBreak)
     where TCase : IEquatable 
  35. {  
  36. return Case(sc, c=>c.Equals(option), other, bBreak);  
  37. }  
  38.  
  39. public static SwithCase, TOther> Case, TOther>
    (this SwithCase, TOther> sc, Predicate predict, TOther other) 
    where TCase : IEquatable 
  40. {  
  41. return Case(sc, predict, other, true);  
  42. }  
  43.  
  44. public static SwithCase, TOther> Case, TOther>
    (this SwithCase, TOther> sc, Predicate predict,
     TOther other, bool bBreak) where TCase : IEquatable 
  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, TOther>
    (this SwithCase, 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是相反的。


新闻标题:浅析C#switch和case
网页网址:http://www.cdxtjz.cn/article/cogeooc.html

联系我们

您好HELLO!
感谢您来到成都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。
  • 电话:028- 86922220 18980695689
  • 商务合作邮箱:631063699@qq.com
  • 合作QQ: 532337155
  • 成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼

小谭建站工作室

成都小谭网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,小谭建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务!

小谭观点

相对传统的成都网站建设公司而言,小谭是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。
我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。