自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析C# switch和case

開發(fā) 后端
這里介紹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  

到現(xiàn)在為止估計大家應該有一個疑問了,原來的C# switch和case中可以使用“break”直接返回,這里是怎么處理的呢?Case還有第三個參數(shù),它用來處理實是否break,為true時break,false時繼續(xù)下一個Case。個人感覺大多數(shù)情況下,符合某個條件后一般不需要繼續(xù)其它的了,所以重載傳入true,即默認break。與C# switch和case是相反的。

【編輯推薦】

  1. C#局部類型介紹
  2. C#固定指針簡單介紹
  3. 淺析C# FTP WebRequest對象
  4. C#分部方法的應用場景
  5. 簡單介紹VB.NET和C#
責任編輯:佚名 來源: 博客園
相關(guān)推薦

2009-08-20 14:45:13

C# Switch語句

2009-08-27 13:50:08

C# StringBu

2024-05-15 08:09:23

2009-08-27 16:18:47

C#類C#結(jié)構(gòu)體

2009-08-26 09:54:45

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-14 17:45:52

C# ArrayLis

2009-08-17 18:34:50

C# ChangeCo

2009-08-25 17:59:49

C#入門

2009-08-12 14:59:09

C#和Java不同點

2009-10-09 09:07:40

C#委托和事件

2009-08-17 18:04:49

C# 枚舉

2010-02-02 17:20:44

C++ switch-

2009-08-20 16:15:19

C# 匿名方法

2009-07-31 14:03:21

C# Format函數(shù)

2009-08-18 09:24:52

C# Anonymou

2009-08-10 17:36:17

C#擴展方法

2009-08-26 13:07:07

C#交錯數(shù)組
點贊
收藏

51CTO技術(shù)棧公眾號