• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

.NET实用扩展方法详解

asp 搞代码 4年前 (2022-01-03) 37次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了.NET实用扩展方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

 /// <summary> /// 去除子字符串 /// </summary> /// 原字符串 /// 要去除的字符串 /// 去除子字符串之后的结果 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> /// 原字符串 /// 要去除的子字符串 /// 去除子字符串之后的结果 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> /// 序列中元素类型 /// 源数据 /// 开始索引(返回时包括) /// 结束索引(返回时包括) /// 子序列 public static IEnumerable SubEnumerable(this IEnumerable source, int startIndex, int endIndex) { if (source == null) yield return default(T); var length = source.Count(); if (startInde<a style="color:transparent">来源gao*daima.com搞@代#码网</a>x <0 || endindex = length || endIndex >= length) throw new ArgumentOutOfRangeException(); var index = -1; foreach (var item in source) { index++; if (index  endIndex) yield break; yield return item; } } 

4. 通过指定键对序列去重, 不必实现IEqualityComparer接口

 /// <summary> /// 通过对指定的值进行比较返回序列中的非重复元素。 /// </summary> /// 序列中元素类型 /// 指定的比较属性类型 /// 源数据 /// 应用于每个元素的转换函数 /// 一个包含源序列中的按指定属性非重复元素 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. 获取序列中重复的元素序列, 原理和去重类似

 /// <summary> /// 通过对指定的值进行比较返回序列中重复的元素 /// </summary> /// 序列中的数据类型 /// 指定的比较属性类型 /// 源数据 /// 应用于每个元素的转换函数 /// 一个包含源序列中的按指定元素的重复元素 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; } } } /// <summary> /// 通过对指定的值进行比较返回序列中重复的元素 /// </summary> /// 序列中的数据类型 /// 指定的比较属性类型 /// 源数据 /// 应用于每个元素的转换函数 /// 一个包含源序列中的按指定元素的重复元素 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; } } } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是.NET实用扩展方法详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:.NET实用扩展方法详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址