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();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章