C# Member initialization question
-
Hi All, What is difference between these two initializations of _obj: 1)
public class Class1 { public Class1() { // // constructor logic here // } private Class2 _obj = null; }
2)public class Class1 { public Class1() { // // constructor logic here // _obj = null; // initialization inside constructor } private Class2 _obj = null; }
Would there be difference between 1) and 2), if Class2 is a class from another referenced dll? -
Hi All, What is difference between these two initializations of _obj: 1)
public class Class1 { public Class1() { // // constructor logic here // } private Class2 _obj = null; }
2)public class Class1 { public Class1() { // // constructor logic here // _obj = null; // initialization inside constructor } private Class2 _obj = null; }
Would there be difference between 1) and 2), if Class2 is a class from another referenced dll?Hi, with your code it is the same because both contain a line private Class2 _obj = null; furthermore an object reference is null by default when it is a class member. There are circumstances where there would be a difference; examples: 1) when _obj has to be initialized to something other than null, and its value is required before an object of Class1 is being instantiated (maybe its needed by some public static methods/properties). 2) when the constructor would fail (throw an exception) and hence not reach the initialization statement. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, with your code it is the same because both contain a line private Class2 _obj = null; furthermore an object reference is null by default when it is a class member. There are circumstances where there would be a difference; examples: 1) when _obj has to be initialized to something other than null, and its value is required before an object of Class1 is being instantiated (maybe its needed by some public static methods/properties). 2) when the constructor would fail (throw an exception) and hence not reach the initialization statement. :)
Luc Pattyn [My Articles] [Forum Guidelines]
Me too thought that the initilizations in 1) and 2) were same. But I had a problem in Design mode when 2) was used. I could solve the problem when 1) was used instead. I am just too confused. I dotn find any reason why it behaves differntly. Let me explain in deatil. MyClass1 is a UserControl class. MyClass1 is to be used in frmMain (Main Form)class. MyClass2 is a class in another dll which is referenced in the project and used in MyClass1. I have two Confusions: Confusion-A) The designer of MyClass1 opens fine even if 1) or 2) is used. Confusion-B) assuming I use method 2) - When I try to add the UserControl class MyClass1 in the designer of frmMain, I get an error that it could not add the control. Mysteriously enough if I use method 1) instead, everything worked fine. Since MyClass2 is in another dll, would it initialize it differently if initialized in constructor or while declaring it? Note: Both ways the _obj is initialized as null.
-
Me too thought that the initilizations in 1) and 2) were same. But I had a problem in Design mode when 2) was used. I could solve the problem when 1) was used instead. I am just too confused. I dotn find any reason why it behaves differntly. Let me explain in deatil. MyClass1 is a UserControl class. MyClass1 is to be used in frmMain (Main Form)class. MyClass2 is a class in another dll which is referenced in the project and used in MyClass1. I have two Confusions: Confusion-A) The designer of MyClass1 opens fine even if 1) or 2) is used. Confusion-B) assuming I use method 2) - When I try to add the UserControl class MyClass1 in the designer of frmMain, I get an error that it could not add the control. Mysteriously enough if I use method 1) instead, everything worked fine. Since MyClass2 is in another dll, would it initialize it differently if initialized in constructor or while declaring it? Note: Both ways the _obj is initialized as null.
ArtiGujare wrote:
Confusion-A) The designer of MyClass1 opens fine even if 1) or 2) is used.
That's good.
ArtiGujare wrote:
Confusion-B) assuming I use method 2)
OK, I am not sure but this is what I expect is happening: - the line private Class2 _obj=null; does not do much: it reserves a 4 (or 8) byte reference which is defaulted to zero; it does not need to know what Class2 actually is, so the compiler will insist on knowing Class2, but at run-time Class2 is not needed, since there is no code corresponding to this line. - the line _obj=null; in the constructor is assigning to a variable of type Class2; at run-time this is the first time Class2 is referenced, so the JIT will try to load and compile it (although the statement does not require any knowledge of Class2, by definition this is when Class2 needs to get initialized). Now at design time (which also runs the constructor), it will fail to find the dll containing Class2 since it searches relative to the location of Visual itself, not your project. This I guess is the issue you must solve, but no I do not know how. Hope this helps. :)
Luc Pattyn [My Articles] [Forum Guidelines]