Detailed explanation of. Net practical extension method

Time:2021-4-7

The specific content of the. Net practical extension method is as follows

1. String conversion to nullable numeric type (int, long, float… Similar)

/// <summary>
  ///Converts a string to a 32-bit integer. If the conversion fails, null is returned
  /// </summary>
  ///< param name = "STR" > converted String < / param >
  ///< returns > converted integer, or 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. Remove substrings

/// <summary>
  ///Remove substring
  /// </summary>
  ///< param name = "STR" > original string < / param >
  ///< param name = "substring" > string to be removed < / param >
  ///< returns > results after substring removal
  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>
  ///Remove substring
  /// </summary>
  ///< param name = "STR" > original string < / param >
  ///< param name = "substrings" > substrings to be removed < / param >
  ///< returns > results after substring removal
  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. Get subsequence

/// <summary>
  ///Get subsequence
  /// </summary>
  ///< typeparam name = "t" > element type in sequence < / typeparam >
  ///< param name = "source" > source data < / param >
  ///< param name = "StartIndex" > start index (including when returned) < / param >
  ///< param name = "endindex" > end index (including when returned) < / param >
  ///< returns > subsequence
  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. It is unnecessary to implement the iequalitycomparer interface by specifying the key to remove the duplicate sequence

/// <summary>
  ///Returns a non repeating element in a sequence by comparing the specified values.
  /// </summary>
  ///< typeparam name = "t" > element type in sequence < / typeparam >
  ///< typeparam name = "tresult" > the specified comparison attribute type < / typeparam >
  ///< param name = "source" > source data < / param >
  ///< param name = "selector" > the conversion function applied to each element < / param >
  ///< returns > a non repeating element containing the specified attributes in the source sequence
  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. Get the repeated element sequence in the sequence. The principle is similar to that of de duplication

/// <summary>
  ///Returns duplicate elements in a sequence by comparing the specified values
  /// </summary>
  ///< typeparam name = "t" > data types in the sequence < / typeparam >
  ///< typeparam name = "tresult" > the specified comparison attribute type < / typeparam >
  ///< param name = "source" > source data < / param >
  ///< param name = "selector" > the conversion function applied to each element < / param >
  ///< returns > a repeating element that contains the specified elements in the source sequence
  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>
  ///Returns duplicate elements in a sequence by comparing the specified values
  /// </summary>
  ///< typeparam name = "t" > data types in the sequence < / typeparam >
  ///< typeparam name = "tresult" > the specified comparison attribute type < / typeparam >
  ///< param name = "source" > source data < / param >
  ///< param name = "selector" > the conversion function applied to each element < / param >
  ///< returns > a repeating element that contains the specified elements in the source sequence
  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;
      }
    }
  }

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.