高手请进来 一般人解决不了
很奇怪的问题?
下面的代码用调试单步能生成3个不同的随机数,但是直接运行,3个随机数相同 ,求解释???
我使用注释的代码运行又能生成3个不同的随机数, 求解释?、、
namespace Computer
{
class Computer
{
public Computer() { }
/// <summary>
/// 价格
/// </summary>
public int Price { get; set; }
/// <summary>
/// 购买日期
/// </summary>
public string Date { get; set; }
/// <summary>
/// 电脑ID
/// </summary>
public string ID { get; set; }
/// <summary>
/// 型号
/// </summary>
public string Type { get; set; }
public void ShowID() {
Random rand = new Random();
this.ID = Type +"--"+ rand.Next(1000,9999);
}
//public void ShowType(Computer[] comp) {
// Random rand = new Random();
// foreach (Computer item in comp)
// {
// item.ID = Type + "--" + rand.Next(1000, 9999);
// }
// }
}
class Program
{
static void Main(string[] args)
{
Computer[] computer = new Computer[] { new Computer(),new Computer(),new Computer()};
computer[0].Price = 10000;
computer[0].Date = "2009-12-09";
computer[0].Type = "HP001";
computer[0].ShowID();
computer[1].Price = 5000;
computer[1].Date = "2010-12-09";
computer[1].Type = "IBM001";
computer[1].ShowID();
computer[2].Price = 8000;
computer[2].Date = "2012-12-09";
computer[2].Type = "IBM002";
computer[2].ShowID();
// computer[0].ShowType(computer);
Console.WriteLine("电脑型号\t价格为\t\t购买日期为\t\t电脑编号");
foreach (Computer item in computer)
{
Console.WriteLine("{0}\t\t{1}\t\t{2}\t\t{3}",item.Type,item.Price,item.Date,item.ID);
}
Console.ReadLine();
}
}