Why C# allows initializing static class variables in non-static contructor? -
why c# allows initializing static class variables in non-static contructor? static variables should allowed initialized on static constructors. ideas?
public class customer { public string name; public customer() { name = "c1"; console.writeline("creating customer " + name); } } class program { public static customer cust; public program() { cust = new customer(); //why allowed i.e. initialize static variable in non-static constructor? } static void main(string[] args) { program program = new program(); program = new program(); console.read(); } }
don't @ initializing, @ setting.
if initialized via static constructor or @ declaration, add readonly
keyword.
e.g.
public readonly static customer cust; //allowed static program() { cust = new customer(); } //not allowed public program() { cust = new customer(); }
Comments
Post a Comment