48 lines
1.1 KiB
C#
48 lines
1.1 KiB
C#
using System.Collections;
|
|
|
|
namespace adas_core.Domain.Utils;
|
|
|
|
public static class CollectionsUtils
|
|
{
|
|
public static bool IsNullOrEmpty<T>(this List<T>? value)
|
|
{
|
|
return value == null || !value.Any();
|
|
}
|
|
|
|
public static bool IsEmptyOrNull<T>(List<T>? value)
|
|
{
|
|
return value == null || !value.Any();
|
|
}
|
|
|
|
public static bool HasElements<T>(List<T>? value)
|
|
{
|
|
return value != null && value.Any();
|
|
}
|
|
|
|
public static bool IsNotEmpty<T>(List<T> value)
|
|
{
|
|
return !IsEmptyOrNull(value);
|
|
}
|
|
|
|
public static List<T> EmptyIfNull<T>(List<T>? value)
|
|
{
|
|
return value ?? [];
|
|
}
|
|
|
|
public static List<T>? NullIfEmpty<T>(List<T> value)
|
|
{
|
|
return IsEmptyOrNull(value) ? null : value;
|
|
}
|
|
|
|
public static Array ToArray(ICollection collection, Type type)
|
|
{
|
|
var result = Array.CreateInstance(type, collection.Count);
|
|
collection.CopyTo(result, 0);
|
|
return result;
|
|
}
|
|
|
|
public static List<string> IfEmptyOrNull(List<string> list, List<string> defaultList)
|
|
{
|
|
return IsEmptyOrNull(list) ? defaultList : list;
|
|
}
|
|
} |