一聚教程网:一个值得你收藏的教程网站

热门教程

.NET实用扩展方法详解

时间:2022-06-25 03:28:16 编辑:袖梨 来源:一聚教程网

持续更新的.NET实用扩展方法,具体内容如下

1. 字符串转换为可空数值类型(int, long, float...类似)

  /// 
  /// 将字符串转换成32位整数,转换失败返回null
  /// 
  /// 转换的字符串
  /// 转换之后的整数,或null
  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. 去除子字符串

  /// 
  /// 去除子字符串
  /// 
  /// 原字符串
  /// 要去除的字符串
  /// 去除子字符串之后的结果
  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);
  }

  /// 
  /// 去除子字符串
  /// 
  /// 原字符串
  /// 要去除的子字符串
  /// 去除子字符串之后的结果
  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. 获取子序列

  /// 
  /// 获取子序列
  /// 
  /// 序列中元素类型
  /// 源数据
  /// 开始索引(返回时包括)
  /// 结束索引(返回时包括)
  /// 子序列
  public static IEnumerable SubEnumerable(this IEnumerable 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接口

  /// 
  /// 通过对指定的值进行比较返回序列中的非重复元素。
  /// 
  /// 序列中元素类型
  /// 指定的比较属性类型
  /// 源数据
  /// 应用于每个元素的转换函数
  /// 一个包含源序列中的按指定属性非重复元素
  public static IEnumerable Distinct(this IEnumerable source, Func selector)
  {
    if (source == null)
      throw new ArgumentNullException(nameof(source));
    if (selector == null)
      throw new ArgumentNullException(nameof(selector));
    var set = new HashSet();
    foreach (var item in source)
    {
      var result = selector(item);
      if (set.Add(result))
      {
        yield return item;
      }
    }
  }

5. 获取序列中重复的元素序列, 原理和去重类似

  /// 
  /// 通过对指定的值进行比较返回序列中重复的元素
  /// 
  /// 序列中的数据类型
  /// 指定的比较属性类型
  /// 源数据
  /// 应用于每个元素的转换函数
  /// 一个包含源序列中的按指定元素的重复元素
  public static IEnumerable Identical(this IEnumerable source)
  {
    if (source == null)
      throw new ArgumentNullException(nameof(source));
    var setT = new HashSet();
    foreach (var item in source)
    {
      if (!setT.Add(item))
      {
        yield return item;
      }
    }
  }

  /// 
  /// 通过对指定的值进行比较返回序列中重复的元素
  /// 
  /// 序列中的数据类型
  /// 指定的比较属性类型
  /// 源数据
  /// 应用于每个元素的转换函数
  /// 一个包含源序列中的按指定元素的重复元素
  public static IEnumerable Identical(this IEnumerable source, Func selector)
  {
    if (source == null)
      throw new ArgumentNullException(nameof(source));
    if (selector == null)
      throw new ArgumentNullException(nameof(selector));
    var setTResult = new HashSet();
    foreach (var item in source)
    {
      var result = selector(item);
      if (!setTResult.Add(result))
      {
        yield return item;
      }
    }
  }

热门栏目