c# - Extension Method not Returning Correct Collection -
i using extension method shuffles generic list. works
public static void shuffle<t>(this ilist<t> list) { rngcryptoserviceprovider provider = new rngcryptoserviceprovider(); int n = list.count; while (n > 1) { byte[] box = new byte[1]; provider.getbytes(box); while (!(box[0] < n * (byte.maxvalue / n))); int k = (box[0] % n); n--; t value = list[k]; list[k] = list[n]; list[n] = value; } }
i trying trying create extension method utilize shuffle(), shuffle items in list in groups based on defined grouping size. method seems work when debugging extension method, source list in calling code still contains original list after extension call:
public static void grouprandomize<t>(this ilist<t> sourcelist, int groupsize) { list<t> shuffledlist = new list<t>(); list<t> templist = new list<t>(); int addcounter = 0; (int = 0; < sourcelist.count; i++) { templist.add(sourcelist[i]); // if we've built total group, or we're done processing entire list if ((addcounter == groupsize - 1) || (i == sourcelist.count - 1)) { templist.shuffle(); shuffledlist.addrange(templist); templist.clear(); addcounter = 0; } else { addcounter++; } } sourcelist = shuffledlist; }
how ensure shuffled list stored source list?
sourcelist local variable. might improve return shuffedlist;
var newlist = caller.grouprandomize<t>(5) ;
c# extension-methods
No comments:
Post a Comment