0

I have a method that's called when I've done with a class I've created, I guess you could treat it like a dispose method, even though it isn't called Dispose(), but something else.

public void Destroy()
{
    Array.Clear(SqState, 0, SqState.Length);
    Array.Clear(SqFloorHeight, 0, SqFloorHeight.Length);
    Array.Clear(SqSeatRot, 0, SqSeatRot.Length);

    _staticModel = null;
    Heightmap = null;
    SqState = null;
    SqFloorHeight = null;
    SqSeatRot = null;
}

I was wondering if I even need to set the objects to null or will GC handle this for me? Their just basic var types like strings, doubles and integers, although _staticModel is another class I've made.

I'm also not sure on if I need to clear the arrays (first 3 lines) and what that will even do? I'm just trying to get my head around disposing and what I should and shouldn't do in this situation.

So, I do apologise if this is a basic question, you're dealing with a dispose newbie here.

ropuxil
  • 121
  • 2
  • 12
  • 1
    As long as you don't need to free unmanaged handles or have allocated much memory, you don't need to do anything. The GC do it for you. See for more here: https://stackoverflow.com/questions/245856/when-should-i-dispose-my-objects-in-net – DogeAmazed Jan 27 '18 at 00:27
  • If linked duplicate is not enough make sure to re-read search result you tried (I assume something like https://www.bing.com/search?q=c%23+gc+set+to+null ) and [edit] post to clarify what else you don't understand. – Alexei Levenkov Jan 27 '18 at 00:36
  • The Array.Clear() method calls don't do anything useful either when the arrays are members of the class. So you can completely remove the method. If you'd name this method Reset() or Clear(), implying that the object stays referenced and might be used again later, then you keep it. None of this has anything to do with IDisposable, that's a different contract. – Hans Passant Jan 27 '18 at 00:47

0 Answers0