溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

.NET實用擴展方法有哪些

發布時間:2021-08-12 11:34:50 來源:億速云 閱讀:149 作者:小新 欄目:開發技術

這篇文章主要為大家展示了“.NET實用擴展方法有哪些”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“.NET實用擴展方法有哪些”這篇文章吧。

1. 字符串轉換為可空數值類型(int, long, float...類似)

  /// <summary>
  /// 將字符串轉換成32位整數,轉換失敗返回null
  /// </summary>
  /// <param name="str">轉換的字符串</param>
  /// <returns>轉換之后的整數,或null</returns>
  public static int? TryParseToInt32(this string str)
  {
    if (string.IsNullOrWhiteSpace(str))
      return null;
    var result = 0;
    if (int.TryParse(str, out result))
      return result;
    else
      return null;
  }

2. 去除子字符串

  /// <summary>
  /// 去除子字符串
  /// </summary>
  /// <param name="str">原字符串</param>
  /// <param name="substring">要去除的字符串</param>
  /// <returns>去除子字符串之后的結果</returns>
  public static string DeSubstring(this string str, string substring)
  {
    if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(substring) || !str.Contains(substring))
    {
      return str;
    }

    return Regex.Replace(str, Regex.Escape(substring), string.Empty);
  }

  /// <summary>
  /// 去除子字符串
  /// </summary>
  /// <param name="str">原字符串</param>
  /// <param name="substrings">要去除的子字符串</param>
  /// <returns>去除子字符串之后的結果</returns>
  public static string DeSubstring(this string str, params string[] substrings)
  {
    if (string.IsNullOrEmpty(str))
      return str;
    if (substrings == null)
      return str;
    var newStr = str;
    foreach (var item in substrings)
    {
      newStr = newStr.DeSubstring(item);
    }
    return newStr;
  }

3. 獲取子序列

  /// <summary>
  /// 獲取子序列
  /// </summary>
  /// <typeparam name="T">序列中元素類型</typeparam>
  /// <param name="source">源數據</param>
  /// <param name="startIndex">開始索引(返回時包括)</param>
  /// <param name="endIndex">結束索引(返回時包括)</param>
  /// <returns>子序列</returns>
  public static IEnumerable<T> SubEnumerable<T>(this IEnumerable<T> source, int startIndex, int endIndex)
  {
    if (source == null)
      yield return default(T);
    var length = source.Count();
    if (startIndex < 0 || endIndex < startIndex || startIndex >= length || endIndex >= length)
      throw new ArgumentOutOfRangeException();

    var index = -1;
    foreach (var item in source)
    {
      index++;
      if (index < startIndex)
        continue;
      if (index > endIndex)
        yield break;
      yield return item;
    }
  }

4. 通過指定鍵對序列去重, 不必實現IEqualityComparer接口

  /// <summary>
  /// 通過對指定的值進行比較返回序列中的非重復元素。
  /// </summary>
  /// <typeparam name="T">序列中元素類型</typeparam>
  /// <typeparam name="TResult">指定的比較屬性類型</typeparam>
  /// <param name="source">源數據</param>
  /// <param name="selector">應用于每個元素的轉換函數</param>
  /// <returns>一個包含源序列中的按指定屬性非重復元素</returns>
  public static IEnumerable<T> Distinct<T, TResult>(this IEnumerable<T> source, Func<T, TResult> selector)
  {
    if (source == null)
      throw new ArgumentNullException(nameof(source));
    if (selector == null)
      throw new ArgumentNullException(nameof(selector));
    var set = new HashSet<TResult>();
    foreach (var item in source)
    {
      var result = selector(item);
      if (set.Add(result))
      {
        yield return item;
      }
    }
  }

5. 獲取序列中重復的元素序列, 原理和去重類似

  /// <summary>
  /// 通過對指定的值進行比較返回序列中重復的元素
  /// </summary>
  /// <typeparam name="T">序列中的數據類型</typeparam>
  /// <typeparam name="TResult">指定的比較屬性類型</typeparam>
  /// <param name="source">源數據</param>
  /// <param name="selector">應用于每個元素的轉換函數</param>
  /// <returns>一個包含源序列中的按指定元素的重復元素</returns>
  public static IEnumerable<T> Identical<T>(this IEnumerable<T> source)
  {
    if (source == null)
      throw new ArgumentNullException(nameof(source));
    var setT = new HashSet<T>();
    foreach (var item in source)
    {
      if (!setT.Add(item))
      {
        yield return item;
      }
    }
  }

  /// <summary>
  /// 通過對指定的值進行比較返回序列中重復的元素
  /// </summary>
  /// <typeparam name="T">序列中的數據類型</typeparam>
  /// <typeparam name="TResult">指定的比較屬性類型</typeparam>
  /// <param name="source">源數據</param>
  /// <param name="selector">應用于每個元素的轉換函數</param>
  /// <returns>一個包含源序列中的按指定元素的重復元素</returns>
  public static IEnumerable<T> Identical<T, TResult>(this IEnumerable<T> source, Func<T, TResult> selector)
  {
    if (source == null)
      throw new ArgumentNullException(nameof(source));
    if (selector == null)
      throw new ArgumentNullException(nameof(selector));
    var setTResult = new HashSet<TResult>();
    foreach (var item in source)
    {
      var result = selector(item);
      if (!setTResult.Add(result))
      {
        yield return item;
      }
    }
  }

以上是“.NET實用擴展方法有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女