3

I have been doing research on Interfaces in C# for some time now,According to MSDN

"Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality."

https://msdn.microsoft.com/en-in/library/3b5b8ezk(v=vs.90).aspx

When implementing Dispose() instead of using Interface IDisposable I can simply define 3 methods of Dispose() & give it to the user.My Question here is "Why has Microsoft created IDisposable interface and what is the purpose of using Interface to implement Dispose()".

This is what I meant

//This method is used to release Managed Resources.
public void Dispose()
{
    this.Dispose();
}

//This method is used to release both managed & unmanaged Resources.
public void DisposeAll()
{
    this.Dispose();
    GC.SuppressFinalize(this);
    ReleaseUnmangedResources();
}

//This method is used to release only unmanaged Resources.
public void DisposeUnmanaged()
{
    ReleaseUnmangedResources();
}

I am sorry if this question is too stupid or simple.Please help me in understanding interfaces.

Arun
  • 145
  • 1
  • 7
  • Allowing users of our class to decide how to dispose of its resources is a really bad idea. Why would you want to expose the fact that you've got managed or unmanaged resources? By just exposing `Dispose` you tell the user that they need to ask you to clean up, but they don't need to know why. – Sean Apr 15 '16 at 11:02
  • Just for your reference, it is a nice read - http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface – Yogi Apr 15 '16 at 11:05
  • More than one reason, but on the top of the list is certainly that .NET does not support multiple inheritance. If you actually need to release unmanaged resources yourself, the wrong thing to do 99.9% of the time, then you should use the disposable pattern and implement a finalizer. The correct thing to do is to wrap them with a SafeHandle class. Quite rare to have to write your own. – Hans Passant Apr 15 '16 at 11:37

2 Answers2

4

IDisposable has special language support. Any object that implements IDisposable can be used as the subject of a using statement.

So,

using(var myDisposable = new ClassThatImplementsIDisposable())
{
      //do some stuff/ even throw an exception
}//myDisposable.Dispose() is automatically called, even if an exception happened.

using statements are a very (very very) useful way to ensure that stuff gets cleaned up without having to write a whole bunch of boilerplate to ensure that it happens (even in the case of exceptions).

By providing the IDisposable interface, you are advertising that the object needs disposing. Without it, disposal might be overlooked, and tools (such as FXCop) will not pick this up.

spender
  • 117,338
  • 33
  • 229
  • 351
0

By implementing IDisposable interface, you are telling the user of your class, that he should call Dispose() method when he is done with the class. So the user is going to do something like this:

DisposableClass c = new DisposableClass();
//doing something
if (c is IDisposable)
  c.Dispose();

Also, IDisposable objects are automatically disposed when created with using statement.

using(var c = new DisposableClass())
{
  //doing something
} //c.Dispose() is called

In this case, Dispose() is called even if an exception was thrown inside the using block.

Fila
  • 193
  • 7