Detailed explanation of practical extension method of. NET
- 2021-10-13 07:07:27
- OfStack
Continuously updated. NET practical extension method, the specific contents are as follows
1. Strings are converted to nullable numeric types (int, long, float... similar)
/// <summary>
/// Converts a string to a 32 Bit integer , Conversion failure returns null
/// </summary>
/// <param name="str"> Converted string </param>
/// <returns> Integer after conversion , 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 substrings
/// </summary>
/// <param name="str"> Original string </param>
/// <param name="substring"> String to remove </param>
/// <returns> Result after removing substrings </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>
/// Remove substrings
/// </summary>
/// <param name="str"> Original string </param>
/// <param name="substrings"> Substring to remove </param>
/// <returns> Result after removing substrings </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. Get a child sequence
/// <summary>
/// Get a sub-sequence
/// </summary>
/// <typeparam name="T"> Element type in sequence </typeparam>
/// <param name="source"> Source data </param>
/// <param name="startIndex"> Start indexing ( Include on return )</param>
/// <param name="endIndex"> End index ( Include on return )</param>
/// <returns> Subsequence </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. You do not have to implement the IEqualityComparer interface by specifying the key pair sequence to deduplicate
/// <summary>
/// Returns a distinct element in a sequence by comparing the specified value.
/// </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"> Transformation function applied to each element </param>
/// <returns>1 Contains non-repeating elements by specified attributes in the source sequence </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. Get the repeated sequence of elements in the sequence, and the principle is similar to de-duplication
/// <summary>
/// Returns duplicate elements in a sequence by comparing specified values
/// </summary>
/// <typeparam name="T"> Data types in sequences </typeparam>
/// <typeparam name="TResult"> The specified comparison attribute type </typeparam>
/// <param name="source"> Source data </param>
/// <param name="selector"> Transformation function applied to each element </param>
/// <returns>1 Contains repeating elements by the specified element in the source sequence </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>
/// Returns duplicate elements in a sequence by comparing specified values
/// </summary>
/// <typeparam name="T"> Data types in sequences </typeparam>
/// <typeparam name="TResult"> The specified comparison attribute type </typeparam>
/// <param name="source"> Source data </param>
/// <param name="selector"> Transformation function applied to each element </param>
/// <returns>1 Contains repeating elements by the specified element in the source sequence </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;
}
}
}