C#中switch和case如何使用

今天就跟大家聊聊有關(guān)C# 中switch和case如何使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)公司專注網(wǎng)站定制,經(jīng)驗豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計體驗!已為崗亭等企業(yè)提供專業(yè)服務。

  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  

看完上述內(nèi)容,你們對C# 中switch和case如何使用有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當前標題:C#中switch和case如何使用
文章路徑:http://bm7419.com/article16/jcesdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版Google、定制開發(fā)、域名注冊App設(shè)計、響應式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)