S
shapper
Hello,
I have 3 methods that:
1 - Return one random record from a list;
2 - Return many random records from a list;
3 - Order a list randomly.
At the moment I am trowing exceptions where the collection is empty or
null but this does not make sense:
1:
public static T Random<T>(this IEnumerable<T> collection) {
if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");
2:
public static IEnumerable<T> Random<T>(this IEnumerable<T>
collection, Int32 size) {
if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");
if (size <= 0) throw new ArgumentOutOfRangeException("size",
"Size must be greater than zero");
3:
public static IEnumerable<T> OrderRandomly<T>(this IEnumerable<T>
collection) {
if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");
For 3, the OrderRandomly, I think I could just return the collection
again:
if (collection == null) return collection;
What should I return on 1 and 2?
I was trying to return null but got errors ...
Thanks,
Miguel
I have 3 methods that:
1 - Return one random record from a list;
2 - Return many random records from a list;
3 - Order a list randomly.
At the moment I am trowing exceptions where the collection is empty or
null but this does not make sense:
1:
public static T Random<T>(this IEnumerable<T> collection) {
if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");
2:
public static IEnumerable<T> Random<T>(this IEnumerable<T>
collection, Int32 size) {
if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");
if (size <= 0) throw new ArgumentOutOfRangeException("size",
"Size must be greater than zero");
3:
public static IEnumerable<T> OrderRandomly<T>(this IEnumerable<T>
collection) {
if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");
For 3, the OrderRandomly, I think I could just return the collection
again:
if (collection == null) return collection;
What should I return on 1 and 2?
I was trying to return null but got errors ...
Thanks,
Miguel