I've seen similar questions, but the answers don't seem to answer what IDispose
really should do.
So far, I have a class that looks like this:
public class ArrayPointsEnumerator : IEnumerator<Vector3Int> {
Vector3Int max;
public ArrayPointsEnumerator(Vector3Int size) {
max = size - Vector3Int.one;
}
Vector3Int point = Vector3Int.zero;
public Vector3Int Current => point;
object IEnumerator.Current => point;
public bool MoveNext() {
// (same functionality as:) // // // // //
// for(int x = 0; x < max.x + 1; x++) //
// for(int y = 0; y < max.y + 1; y++) //
// for(int z = 0; z < max.z + 1; z++) //
if(point.z < max.z) point.z++;
else {
point.z = 0;
if(point.y < max.y) point.y++;
else {
point.y = 0;
if(point.x < max.x) point.x++;
else return false;
}
}
return true;
}
public void Reset() => point = Vector3Int.zero;
}
Which is meant to be used like this:
ClassWithArray {
object[,,] array;
Vector3Int Size => /*array.GetLength(0 through 2)*/;
public ArrayPointsEnumerator ArrayPoints => ArrayPointsEnumerator(Size);
}
...
foreach(Vector3Int point in ClassWithArray.ArrayPoints {
//do stuff
}
But it's missing an IDispose
method, which Visual Studio is giving me two solutions for.
(Both of these are the unedited "quick fixes" Visual Studio provies.)
Implement interface.
public void Dispose() {
throw new NotImplementedException();
}
Implement interface with Dispose pattern.
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing) {
if(!disposedValue) {
if(disposing) {
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~ArrayPointsEnumerator()
// {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
public void Dispose() {
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
And honestly, maybe I'm just being really dumb here, but even after looking at the Microsoft Docs for Implementing a Dispose method and IDisposable Interface I'm confused on what Dispose()
should do. I've also read answers to similar questions, (again maybe I'm just being dumb or just missing something) but they don't seem to explain what Dispose()
is.