C#/.NET Lit随机打乱——扩展方法封装

    public static void RandomSort<T>(this List<T> @this)
    {
        var random = new Random();
        var newList = new List<T>();
        foreach (var item in @this)
        {
            newList.Insert(random.Next(newList.Count), item);
        }
        for (int i = 0; i < @this.Count; i++)
        {
            @this[i] = newList[i];
        }
    }

由于使用的是this扩展

调用的时候只需要直接.方法

listTest.RandomSort();

发布了190 篇原创文章 · 获赞 298 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/qq_34202873/article/details/98057604