In your example, the recommended would be that your interface implementasse Idisposable:
public interface IClassBase<TEntity> : IDisposable where TEntity : class
{
void Add(TEntity obj);
}
This way you force classes to implement your interface, also implement Idisposable.
On the other hand, maybe that doesn’t make much sense, because Idisposable is usually implemented by classes that use native methods and need to release manually used resources, which will not be released by Garbage Collecor.
So what you need to know is: It makes sense for you to create an interface that forces you to implement Dispose?
I usually don’t care about that, I let whoever is implementing my interface decide whether or not they need Idisposable, and in that case, I implement Idisposable directly in the class. Remembering that it is recommended to implement Dispose in set with the finalizer class. Because Garbage Collector does not call Dispose, only the class finisher.
Remembering also another resource quite useful when a class implements Idisposable, is the possibility to use the object within a block using. Ensuring correct use of Idisposable by calling Dispose automatically at the end even if 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 particularities, like the possibility of using the block. And as I said, the implementation of the interface is another "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 Idisposable.Dispose. Simply 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
*/
}
}
}
Although her answer is correct, she makes no sense for what was asked.
– Maniero
Explain why not, and I can update.
– Bruno Piovan
Where are you saying about use redundancy?
– Maniero
I didn’t understand the question exactly about "redundancy", but about whether or not to implement the Idisposable interface. And in my answer I try to explain a little bit about use. The fact that the method is called Dispose does not make the implementation of the Idisposable interface exactly redundant, since the method created with this name could have any other.
– Bruno Piovan
The question is very clear that it is about redundancy. That’s exactly what I said, you explained something else, not what was asked. It’s in the title saying it’s about redundancy, it talks about it in the first paragraph, and the last paragraph where the question is asked describes a redundancy.
– Maniero
okay.............
– Bruno Piovan