mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-16 01:41:14 +08:00
651d0d0e14
* 当array==null时,改为返回Array.Empty<T>() * 考虑T类型为引用类型时,集合元素可能为null,原有逻辑会触发null.Equals()的问题
81 lines
1.8 KiB
C#
81 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AntDesign
|
|
{
|
|
public static class EnumerableExtensions
|
|
{
|
|
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
|
|
{
|
|
if (items == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (T obj in items)
|
|
{
|
|
action(obj);
|
|
}
|
|
}
|
|
|
|
public static async Task ForEachAsync<T>(this IEnumerable<T> items, Func<T, Task> func)
|
|
{
|
|
if (items == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var item in items) await func(item);
|
|
}
|
|
|
|
public static bool IsIn<T>(this T source, params T[] array)
|
|
{
|
|
if (array == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return array.Contains(source);
|
|
}
|
|
|
|
public static bool IsIn<T>(this T source, IEnumerable<T> array)
|
|
{
|
|
if (array == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return array.Contains(source);
|
|
}
|
|
|
|
public static T[] Append<T>(this T[] array, T item)
|
|
{
|
|
if (array == null)
|
|
{
|
|
return new[] { item };
|
|
}
|
|
Array.Resize(ref array, array.Length + 1);
|
|
array[^1] = item;
|
|
|
|
return array;
|
|
}
|
|
|
|
public static T[] Remove<T>(this T[] array, T item)
|
|
{
|
|
if (array == null)
|
|
{
|
|
return Array.Empty<T>();
|
|
}
|
|
|
|
if (item == null)
|
|
{
|
|
return array.Where(x => x != null).ToArray();
|
|
}
|
|
|
|
return array.Where(x => !item.Equals(x)).ToArray();
|
|
}
|
|
}
|
|
}
|