使用扩展方法的集合随机化

Rus*_*sel 4 c# extension-methods

可能重复:
C#:使用Random和OrderBy是一个很好的shuffle算法吗?

我想创建一个扩展方法,该方法应该对集合中的项进行随机播放.

我能改进以下内容吗?

public static IList<T> RandomList<T>(this IList<T> source)
{
   if (source.Count <= 0) throw new ArgumentException("No Item to Randomize");  

            for (int i =source.Count-1 ; i>0; i--)
            {
                int RandomIndex = Rnd.Next(i + 1);
                T temp = source[i];
                source[i] = source[RandomIndex];
                source[RandomIndex] = temp;
            }

            return source;
 }
Run Code Online (Sandbox Code Playgroud)

BFr*_*ree 5

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
{
   foreach(var item in source.OrderBy(i => Guid.NewGuid()))
   {
      yield return item;
   }
}
Run Code Online (Sandbox Code Playgroud)