服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - .NET实用扩展方法详解

.NET实用扩展方法详解

2020-05-16 17:19A_ning ASP.NET教程

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/// <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. 去除子字符串

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/// <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. 获取子序列

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <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接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/// <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. 获取序列中重复的元素序列, 原理和去重类似

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// <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;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/aning2015/archive/2017/10/13/7660160.html

延伸 · 阅读

精彩推荐