新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

.NET实用扩展方法有哪些-创新互联

这篇文章主要为大家展示了“.NET实用扩展方法有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“.NET实用扩展方法有哪些”这篇文章吧。

成都创新互联公司成立于2013年,先为宝坻等服务建站,宝坻等地企业,进行企业商务咨询服务。为宝坻企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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;
      }
    }
  }

以上是“.NET实用扩展方法有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网页名称:.NET实用扩展方法有哪些-创新互联
文章转载:http://www.wjwzjz.com/article/cdjpph.html
在线咨询
服务热线
服务热线:028-86922220
TOP