In your example, we recommend that your interface implement IDisposable:
public interface IClassBase<TEntity> : IDisposable where TEntity : class
{
void Add(TEntity obj);
}
In this way you force classes to implement your interface, also implement IDisposable.
On the other hand, this may not make much sense, since IDisposable is usually implemented by classes that use native methods and need to release the resources used manually, which will not be freed by Garbage Collecor.
So what you need to know is: Does it make sense for you to create an interface that forces the implementation of Dispose?
I usually do not care about this, I leave it to anyone to implement my interface to decide whether or not I need IDisposable, and in that case, I implement IDisposable directly in the class. Remembering that it is recommended to implement Dispose in the set with the class finalizer . For the Garbage Collector does not call the Dispose, just the class finalizer.
Also remembering another very useful feature when a class implements IDisposable, is the possibility of using the object within a block using . Ensuring the correct use of IDisposable by calling Dispose automatically at the end even though an error occurs.
UPDATE :
since the answer is not clear, so here goes:
No. It is not redundant. The method name is just a name. As I explained above, the IDisposable interface has these peculiarities, such as the possibility of using the using block. And as I said, the implementation of the interface is more a "hint" that the class uses native code that needs to release something manually.
You can have your own Dispose method and you can also implement the IDisposable.Dispose. Just implement the interface explicitly:
using System;
namespace ConsoleApplication1
{
internal interface IMyDisposable
{
void Dispose();
}
internal class MyDisposable : IMyDisposable, IDisposable
{
public void Dispose()
{
Console.WriteLine("Dispose da interface IMyDisposable");
}
void IDisposable.Dispose()
{
Console.WriteLine("método implementando explicitamente Dispose da interface IDisposable");
}
}
internal class Program
{
private static void Main(string[] args)
{
using (var m = new MyDisposable())
{
m.Dispose();
}
//ou então:
var m2 = new MyDisposable();
m2.Dispose();
((IDisposable)m2).Dispose();
/*
print:
Dispose da interface IMyDisposable
método implementando explicitamente Dispose da interface IDisposable
*/
}
}
}