Skip to content

Commit

Permalink
MyCollections.NotNullOrEmpty() extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Deadcows committed Aug 6, 2021
1 parent c711ebe commit f7d3b21
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions Extensions/MyCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,37 +92,39 @@ public static T GetRandom<T>(this IEnumerable<T> collection)
return collection.ElementAt(UnityEngine.Random.Range(0, collection.Count()));
}


#region IsNullOrEmpty and NotNullOrEmpty

/// <summary>
/// Is array null or empty
/// </summary>
public static bool IsNullOrEmpty<T>(this T[] collection)
{
if (collection == null) return true;

return collection.Length == 0;
}
public static bool IsNullOrEmpty<T>(this T[] collection) => collection == null || collection.Length == 0;

/// <summary>
/// Is list null or empty
/// </summary>
public static bool IsNullOrEmpty<T>(this IList<T> collection)
{
if (collection == null) return true;

return collection.Count == 0;
}
public static bool IsNullOrEmpty<T>(this IList<T> collection) => collection == null || collection.Count == 0;

/// <summary>
/// Is collection null or empty. IEnumerable is relatively slow. Use Array or List implementation if possible
/// </summary>
public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection)
{
if (collection == null) return true;

return !collection.Any();
}
public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection) => collection == null || !collection.Any();

/// <summary>
/// Collection is not null or empty
/// </summary>
public static bool NotNullOrEmpty<T>(this T[] collection) => !collection.IsNullOrEmpty();

/// <summary>
/// Collection is not null or empty
/// </summary>
public static bool NotNullOrEmpty<T>(this IList<T> collection) => !collection.IsNullOrEmpty();

/// <summary>
/// Collection is not null or empty
/// </summary>
public static bool NotNullOrEmpty<T>(this IEnumerable<T> collection) => !collection.IsNullOrEmpty();

#endregion


/// <summary>
Expand Down

0 comments on commit f7d3b21

Please sign in to comment.