I type the following code in the 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);
}
}
}
}
Then click the "Debug" button, I found the results very strange:
Output - The same random number
Then, I tried to click the "Run to Cursor" button and the results became different:
Output - Different random numbers
Why?