C # - create a class that implements IDispose

2

please collaborate on the following:

I have a Class class with N attributes:

public class Clase
{
    private string atrib1;
    private int atrib 2;
}

I want that when I use the class I can do (I think for a better management of resources) a:

using(Clase obj = new Clase())
{
    ..........
}

but it does not work that simple, right? I have to implement the IDispose interface (Class: IDispose) and explicitly implement the Dispose () method.

Well now if the million-dollar question comes: what code should I put in the Dispose () method so that both attribute attribute1 and attribute attribute2 are destroyed, released, etc., and also be destroyed, released, etc. the same object in question ???

public void Dispose()
{
    ???
}

Thanks in advance.

    
asked by RSillerico 08.03.2017 в 19:49
source

2 answers

2

In your case, it does not make sense for the class to implement the IDisposable interface, because the only members your class has are type String and int . The members of these types, and the instance of your own class, do not require that you "destroy" yourself as it happens in other programming environments. In fact, in .NET you can not do it. This is automatically handled by the Garbage Collector of .NET Runtime. Let him take care of it for you.

Actually, it only makes sense to implement the interface in one of 2 cases. Out of those 2 cases (as is the case with you), you do not need the IDisposable interface nor do you need to use the using notation to create and use an instance of your class:

  • One or more members of your class has a direct "handle" to a resource outside of the .NET runtime. (examples: network connections, handle to an open file on the hard disk, other Win32 handles, etc.) In reality, this case is extremely rare (and more complicated, because then you have to worry about defining a finalizer too, but there's no need to expand on that topic here), since you usually do not need to handle these "handles" directly in your classes, but rather, you do it using existing classes in the Framework that handle these low-level things for you, which brings us to the second case ...
  • One or more of the members of your class in turn implements the interface IDisposable (examples: SqlConnection , Stream , etc.). In that case, the Dispose() method should execute the respective .Dispose() of the members that implement IDispose .

    Example:

    public class Clase : IDisposable
    {
        private string atrib1;
        private int atrib 2;
        private Stream stream; // Stream implementa IDisposable
    
        private bool disposed;
    
        public void Dispose()
        {
            // para evitar hacer el dispose 2 veces por nada.
            if (disposed) return;
    
            disposed = true;
    
            // Aquí llamas el ".Dispose" de todos los miembros que
            // implementan IDisposable, en este caso, solo "stream".
            // Nota que uso el operador ?. para manejar el caso cuando
            // "stream == null".
            stream?.Dispose();
        }
    }
    

    In passing, it is worth mentioning that this example uses a simplified version of implementing IDisposable compared to what is suggested in the official documentation . Personally, I consider that this simplified pattern is perfectly acceptable for 99% of the cases where you do need to implement the interface. Actually, you only need the pattern suggested by Microsoft if 1) you run into the first case above (extremely rare), or 2) if your class inherits from another class, and the various levels of inheritance need to cooperate together to execute the Dispose() .

  • answered by 08.03.2017 / 20:29
    source
    0
    // IDisposable requiere que implementes esta función en tu clase.
    public void Dispose()
    {
        Dispose(true);
        // La siguiente línea se utiliza si el finalizador está sobreescrito (override) en otro pedazo del código.
        GC.SuppressFinalize(this);
    }
    

    You can find examples here: link

        
    answered by 08.03.2017 в 20:18