In C#,
A a = new A();
A a = null;
A a;
How does these 3 lines work with respect to memory?
I know the first line will create a memory in heap, but what about rest two lines?
How it work if, A a ; is a field and local variable.
A
and assigns it to the variable a
.null
to a reference a
. If a
is not used, the compiler might optimize it away.A a = default(A);
which is the same as 2 since default(A)
is null
. For method variables it will show you a warning or error if you don't assign it. This one can too be optimized away if not used.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With