日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析C#switch和case

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

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),嶗山企業(yè)網(wǎng)站建設(shè),嶗山品牌網(wǎng)站建設(shè),網(wǎng)站定制,嶗山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,嶗山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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

 
 
 
  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  

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


分享標(biāo)題:淺析C#switch和case
新聞來源:http://m.5511xx.com/article/cogeooc.html