Files
UtilityLibrary/CollectionUtils.cs
2025-12-14 18:41:56 +03:00

36 lines
901 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace First_week
{
public static class CollectionUtils
{
public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection)
{
return collection == null || !collection.Any();
}
public static List<T> ToSafeList<T>(this IEnumerable<T> collection)
{
return collection?.ToList() ?? new List<T>();
}
public static void Shuffle<T>(this IList<T> list)
{
if (list == null)
return;
Random rng = new Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
}