public static class MyClass
{
// why it is allowed ?
public static Random r = new Random();
// We receive error can not declare instance members in a static class
public static int someVal=new int();
}
Why is it allowed in C# to have the following declaration valid in static class context ?
public static Random r = new Random();
Update :
The code is working properly.So no issues.Thanks for your support.
As is, your code compiles just fine.
You’ll only receive the error you described if you delete the static from
such as in
That happens because all members of a static class have to be static too.