Friday, 15 April 2011

nested - Null Object Ref when instantiating a container class in C# -



nested - Null Object Ref when instantiating a container class in C# -

i have problems understand next behavior of c#: if define class containing nested class this:

public class container { public contained instanceofcontained; public container() { this.instanceofcontained = new contained(); } }

and seek instantiate writting containerinstance = new container(), value of instanceofcontained null. why so, since reference contained object created in constructor written instanceofcontained property of containerinstance object, after constructor finished reference remain garbage collector shouldn't delete instanceofcontained object? if define constructor this:

public class container { public contained instanceofcontained; public container(contained instanceofcontainedref) { this.instanceofcontained = instanceofcontainedref; } }

and instantiate container class this:

containedinstance = new contained(); containerinstance = new container(containedinstance);

everything o.k. can please explain me?

c# nested nullreferenceexception instantiation

No comments:

Post a Comment