您的位置:首页 > 其它

集合随机打乱顺序

2017-03-23 20:05 176 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random ran = new Random();
List<model> newList = new List<model>();
newList.Add(new model { name="A"});
newList.Add(new model { name = "B" });
newList.Add(new model { name = "C" });
newList.Add(new model { name = "D" });
int index = 0;
var temp =new model();
for (int i = 0; i < newList.Count; i++)
{

index = ran.Next(0, newList.Count - 1);
if (index != i)
{
temp = newList[i];
newList[i] = newList[index];
newList[index] = temp;
}
}
foreach (var item in newList)
{
Console.Write(item.name);
}
Console.ReadLine();

}
}
public class model
{
public string name { get; set; }
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐