Search code examples
c#variablesmemorymemory-managementunassigned-variable

memory for unassigned variables in C#


//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?


Solution

  • 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).