Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does .Net do when you declare an object without an instance?

I wonder to know how the .Net Framework handles the declared but not instantiated object situation.

For example i declare an object like

DropDownList ddl;

and do nothing about it. I know that i should do something with this variable and get a warning about it, but what i don't know is the where it will be stored.

Is there a lookup table that stores the data of all declared variables? Or is there a virtual reference for every declaration?

Edit : I just wanted to know how the memory allocated for this object declaration.

Edit2 : Whether it's a local variable or not, i'm just talking about the memory allocation structure. I wonder to know where this references stored.

like image 260
Bedir Yilmaz Avatar asked Apr 14 '26 14:04

Bedir Yilmaz


1 Answers

If ddl is a field, then the value of ddl will be null, as it is a reference type.

Any attempt to call a member on it will result in a NullReferenceException.

If it is a local variable it will simply be unassigned.

Value types will get the default(T) of their type.

The compiler itself may remove the call completely, depending on where it was declared, but this is an implementation detail.

like image 110
Oded Avatar answered Apr 16 '26 02:04

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!