-4
I型在VS 2017年将下面的代码:随机行为:重复相同数量
namespace Test {
class MyClass {
public static Random randomkey;
static MyClass() {
randomkey = new Random();
}
public MyClass() {
randomkey = new Random();
}
public int returnkey() => randomkey.Next();
}
class Program {
private static void Main(string[] args) {
try {
Console.WriteLine(MyClass.randomkey.Next());
var x = new MyClass();
Console.WriteLine(x.returnkey());
var y = new MyClass();
Console.WriteLine(y.returnkey());
Console.ReadLine();
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
}
}
然后单击 “调试” 按钮,我发现结果很奇怪:
Output - The same random number
于是,我试着点击“运行至光标位置”按钮,结果成了不同:
Output - Different random numbers
为什么?
请不要使用无关标签的垃圾邮件。 –