首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

C# 高頻擴展方法庫精選:集合、字符串、調試、範圍判斷、Switch增強、遞歸遍歷、正則、特殊字符串

     編輯:關於.NET

集合與字符串

ExpandAndToString

public static string ExpandAndToString(this IEnumerable src, string separator)
    => string.Join(separator, src.Cast<object>().Select(o => o.ToString()));

// 用法
new[] {1,5,33,14,556}.ExpandAndToString("-") // "1-5-33-14-556"

IsNullOrEmpty / IsNullOrEmptyThen

public static bool IsNullOrEmpty(this string s) => string.IsNullOrEmpty(s);

// 條件執行 Func
public static string IsNullOrEmptyThen(this string s, Func<string,string> func)
    => s.IsNullOrEmpty() ? func(s) : s;

// 條件執行 Action
public static void IsNullOrEmptyThen(this string s, Action<string> act) {
    if (s.IsNullOrEmpty()) act(s);
}

// 用法
"".IsNullOrEmptyThen(f => "default") // "default"
"".IsNullOrEmptyThen(f => MessageBox.Show("empty"))

FormatWith (字符串格式化簡寫)

public static string FormatWith(this string fmt, params object[] args)
    => string.Format(fmt, args);

// 用法
"i:{0:0%};x:{1};".FormatWith(0.35, 200) // "i:35%;x:200;"

In / InRange (集合包含與範圍判斷)

public static bool In<T>(this T t, params T[] set) => set.Contains(t);
public static bool In<T,C>(this T t, Func<T,C,bool> pred, params C[] set)
    => set.Any(c => pred(t, c));

public static bool InRange<T>(this IComparable<T> t, T min, T max)
    => t.CompareTo(min) >= 0 && t.CompareTo(max) <= 0;

// 用法
95.In(31,3,55,67,95,12,4) // true
"b".InRange("a", "c") // true
95.InRange(15, 100) // true

調試神器:Trace 系列 (不打斷鏈式調用)

public static T Trace<T>(this T t, string category = null) {
    Debug.WriteLine($"{category ?? "Trace"}: {t}");
    return t;
}
public static T Trace<T>(this T t, Func<T,object> selector, string category = null) {
    Debug.WriteLine($"{category ?? "Trace"}: {selector(t)}");
    return t;
}
public static T TraceFormat<T>(this T t, string fmt, string category = null) {
    Debug.WriteLine($"{category ?? "Trace"}: {string.Format(fmt, t)}");
    return t;
}

// 用法
"abcdefg".Trace(f => f.ToUpper(), "expr").Remove(4).Trace("normal")
// 輸出: expr: ABCDEFG → normal: abcd

增強版 Switch/Case (鏈式模式匹配)

// 基礎結構
public class Switch<T> { ... }
public class Case<T,R> { ... }

// 用法
15.Switch()
  .CaseRun(15, f => Msg("=15"), false)
  .CaseRun(f => f > 0, f => Msg(">0"))
  .CaseRun(f => f < 0, f => Msg("<0"))
  .DefaultRun(f => Msg("=0"));

var o = 'c'.Switch()
  .CaseReturn('a', 1)
  .CaseReturn('b', 2)
  .CaseReturn('c', 3)
  .CaseReturn('d', 4)
  .CaseReturn(f => f > 'd', 5)
  .DefaultReturn(0).ReturnValue; // 3

遞歸遍歷:RecursionSelect / RecursionEachSelect

// 單對象遞歸
public static IEnumerable<T> RecursionSelect<T>(this T root, Func<T,IEnumerable<T>> children, Predicate<T> filter = null)

// 集合遞歸
public static IEnumerable<T> RecursionEachSelect<T>(this IEnumerable<T> roots, Func<T,IEnumerable<T>> children, Predicate<T> filter = null)

// 用法:獲取所有有子目錄的目錄
new DirectoryInfo(@"C:\Downloads").RecursionSelect(d => d.GetDirectories(), d => d.GetDirectories().Length > 0)

正則表達式封裝

public static bool RegexIsMatch(this string s, string pattern, RegexOptions opt = RegexOptions.None)
    => Regex.IsMatch(s, pattern, opt);
public static Match RegexMatch(this string s, string pattern, RegexOptions opt = RegexOptions.None)
    => Regex.Match(s, pattern, opt);
public static MatchCollection RegexMatches(this string s, string pattern, RegexOptions opt = RegexOptions.None)
    => Regex.Matches(s, pattern, opt);
public static string RegexReplace(this string s, string pattern, string repl, RegexOptions opt = RegexOptions.None)
    => Regex.Replace(s, pattern, repl, opt);
public static string[] RegexSplit(this string s, string pattern, RegexOptions opt = RegexOptions.None)
    => Regex.Split(s, pattern, opt);

特殊字符串類型標記 (語義化格式化)

public static PathString AsPathString(this string s) => new PathString(s);
public static UriString AsUriString(this string s) => new UriString(s);
public static XmlString AsXmlString(this string s) => new XmlString(s);
// 等...

// 用法
@"C:\abc\".AsPathString().Combine(@"D:\1.exe".AsPathString().FileName) // "C:\abc\1.exe"

下載地址

文章來源:skyd.cnblogs.com

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved