gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:36:27 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
C#: Is using Random and OrderBy a good shuffle algorithm?

我想创建一个扩展方法来打乱集合中的项目。

我可以改进以下内容吗?

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;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com