Is it possible to delete member variable? C#

Hi,

Lets say that I have a variable.
The variable only needed to be use in Awake().
And then it will never got used again.

Since having a variable take up space, is it possible to delete that variable during runtime?

Or does C# automatically do magical stuffs for us that I am not aware of?

public class foo
{
     bool b = false;

     void Awake()
     {
          if(b) DoSomething();
          //b is never use again

           //will b = null do the trick?
     }
}

There’s no such thing as getting rid of a member.

Since you only need it in a certain scope, why not just declare it in that scope? (Why make it a member in the first place?)

void Awake()
{
   bool b = someCondition;
   if(b) DoSomething();     
}

‘b’ is allocated memory on your stack, when we reach the end of Awake’s scope, ‘b’ will die (its memory will get reclaimed) - For more info, see variable scope.

“will b = null do the trick?”

Of course not, you can’t do that, ‘b’ is a bool (value-type, which means you can’t null it) you can only null reference types. See value types VS ref types.

“Or does C# automatically do magical stuffs for us that I am not aware of?”

Well yes it does but not for value types. If you lose all references to an object allocated in the heap memory, that object will get garbage collected eventually.
For example:

1- GameObject go = new GameObject();
2- GameObject otherRef = go;
3- go = null;

In the first line, we instantiate a new gameOjbect in the heap memory, so now our ‘go’ (which is sitting in the stack) is referencing that object.
We then create another reference, and let it reference what ‘go’ is referring to (the same gameObject, so now it has 2 references to it)
In the third line we make ‘go’ refer to null (which just means, don’t refer to anything)
so now we only have one reference to our gameObject (no garbage collection so far) - However, if we do something like:

otherRef = anotherGo;

Now, our old gameObject doesn’t have any reference to it, it’s lost, now it’s marked for garbage collection. (If you were in C++ this is called a memory leak, where something gets lost, nothing is referencing it, no garbage collection in C++)

For more info, see this and this.

Use a temporary variable. When you define a variable you also define the scope of the variable. If you define it directly in the class, then the scope is the entire class, but you can also define it inside a method. A variable defined inside a method will have a scope of the remainder of that method. So, if I write this:

    public class foo
    {
        bool b = false;
     
        void Awake()
        {
            b = true;
        }
        
        void Start()
        {
            Debug.Log(b);
        }
    }

This will be fine, and will log the value “true”. However, if I write this:

    public class foo
    {
        void Awake()
        {
            bool b = true;
        }
        
        void Start()
        {
            Debug.Log(b);
        }
    }

I will get a compiler error, because the boolean b does not exist outside the method Awake, so when I try to read it in Start, it can’t find anything with that name.