今天就跟大家聊聊有關(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è)服務。
public static class SwithCaseExtension
{
SwithCase#region SwithCase
public class SwithCase<TCase, TOther>
{
public SwithCase(TCase value, Action<TOther> action)
{
Value = value;
Action = action;
}
public TCase Value { get; private set; }
public Action<TOther> Action { get; private set; }
}
#endregion
Swith#region Swith
public static SwithCase<TCase, TOther> Switch<TCase, TOther>
(this TCase t, Action<TOther> action) where TCase : IEquatable<TCase>{
return new SwithCase<TCase, TOther>(t, action);
}
public static SwithCase<TCase, TOther> Switch<TInput, TCase, TOther>
(this TInput t, Func<TInput, TCase> selector, Action<TOther> action)
where TCase : IEquatable<TCase>{
return new SwithCase<TCase, TOther>(selector(t), action);
}
#endregion
Case#region Case
public static SwithCase<TCase, TOther> Case<TCase, TOther>
(this SwithCase<TCase, TOther> sc, TCase option, TOther other)
where TCase : IEquatable<TCase>{
return Case(sc, option, other, true);
}
public static SwithCase<TCase, TOther> Case<TCase, TOther>
(this SwithCase<TCase, TOther> sc, TCase option, TOther other, bool bBreak)
where TCase : IEquatable<TCase>{
return Case(sc, c=>c.Equals(option), other, bBreak);
}
public static SwithCase<TCase, TOther> Case<TCase, TOther>
(this SwithCase<TCase, TOther> sc, Predicate<TCase> predict, TOther other)
where TCase : IEquatable<TCase>{
return Case(sc, predict, other, true);
}
public static SwithCase<TCase, TOther> Case<TCase, TOther>
(this SwithCase<TCase, TOther> sc, Predicate<TCase> predict,
TOther other, bool bBreak) where TCase : IEquatable<TCase>{
if (sc == null) return null;
if (predict(sc.Value))
{
sc.Action(other);
return bBreak ? null : sc;
}
else return sc;
}
#endregion
Default#region Default
public static void Default<TCase, TOther>
(this SwithCase<TCase, TOther> sc, TOther other){
if (sc == null) return;
sc.Action(other);
}
#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)