偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

淺析C# switch和case

開發(fā) 后端
這里介紹C# switch和case的擴(kuò)展,大家評(píng)價(jià)各不相同,其實(shí)本人也感覺(jué)有點(diǎn)牽強(qiáng)。其中舉了一個(gè)Swith擴(kuò)展的應(yīng)用,今天突然有了新想法,對(duì)它改進(jìn)了一些。

C# switch和case的擴(kuò)展,大家評(píng)價(jià)各不相同,其實(shí)本人也感覺(jué)有點(diǎn)牽強(qiáng)。其中舉了一個(gè)Swith擴(kuò)展的應(yīng)用,今天突然有了新想法,對(duì)它改進(jìn)了一些。所謂“語(yǔ)不驚人死不休”,且看這次的改進(jìn)如何。

我先把擴(kuò)展的源代碼貼出來(lái),折疊一下,等看完后面的例子和講解再回來(lái)看。

  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)在為止估計(jì)大家應(yīng)該有一個(gè)疑問(wèn)了,原來(lái)的C# switch和case中可以使用“break”直接返回,這里是怎么處理的呢?Case還有第三個(gè)參數(shù),它用來(lái)處理實(shí)是否break,為true時(shí)break,false時(shí)繼續(xù)下一個(gè)Case。個(gè)人感覺(jué)大多數(shù)情況下,符合某個(gè)條件后一般不需要繼續(xù)其它的了,所以重載傳入true,即默認(rèn)break。與C# switch和case是相反的。

【編輯推薦】

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

2009-08-20 14:45:13

C# Switch語(yǔ)句

2009-08-27 13:50:08

C# StringBu

2024-05-15 08:09:23

2009-08-07 17:25:37

C# SortedLi

2009-08-27 16:18:47

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

2009-08-26 09:54:45

C#打印預(yù)覽C#打印

2009-08-31 09:20:37

C#事件注冊(cè)和注銷

2009-09-10 16:38:43

C# get set用

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不同點(diǎn)

2009-10-09 09:07:40

C#委托和事件

2009-08-17 18:04:49

C# 枚舉

2009-08-18 09:24:52

C# Anonymou

2009-08-27 13:30:11

C# interfac

2009-08-12 15:20:21

C#事件處理

2009-08-17 13:34:02

C#異步操作

2009-08-26 13:07:07

C#交錯(cuò)數(shù)組

2009-08-21 17:24:06

C# SingleIn
點(diǎn)贊
收藏

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