Reputation: 5301
//Consider this declaration
string name;
Here string variable name is an unassigned variable,does this declaration reserve any memory for name if it does not initialised?
Upvotes: 3
Views: 745
Reputation: 35477
It is not unassigned. All classes/structs receive their default value. For a string it is null
.
If it is a local variable, then optimisation will tend to remove it. If its an instance variable then memory will be allocated (I think, C# spec is unclear).
Upvotes: 4
Reputation: 13138
A variable local to a method doesn't reserve any memory, registers are allocated to it depending on how it is used and how other variables are used. As long as it's not used, no register is allocated to it.
You can have a large number of variables inside a method but there is a limited number of register in your CPU so the compiler optimize your code to re-use registers. For more information, see Register allocation.
No, string name;
doesn't reserve any memory.
Upvotes: -1