ExtensionMethods.cs 728 B

123456789101112131415161718192021222324252627282930
  1. namespace Shared.Extensions
  2. {
  3. public static class HelperExtensions
  4. {
  5. private static Random rng = new Random();
  6. public static T ValueOrDefault<T>(this object value)
  7. {
  8. if (value == null || value.ToString() == "")
  9. {
  10. return default;
  11. }
  12. return (T)Convert.ChangeType(value, typeof(T));
  13. }
  14. public static void Shuffle<T>(this IList<T> list)
  15. {
  16. int n = list.Count;
  17. while (n > 1)
  18. {
  19. n--;
  20. int k = rng.Next(n + 1);
  21. T value = list[k];
  22. list[k] = list[n];
  23. list[n] = value;
  24. }
  25. }
  26. }
  27. }