Using statement gives error on implementation class if Interface does not implement IDisposable

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have the following interface and class:
<pre class="prettyprint public interface IProductServices
{
//Properties, methods and attributes
}

public class ProductServices : IProductServices, IDisposable
{
//Implementation

private bool _disposed = false;

protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
this._disposed = true;
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}[/code]
Now with the using statement in C#:
<pre class="prettyprint IProductServices productServices;

using (productServices = new ProductServices())
{
//some code
}[/code]
I obtain an error:
Error 54 type used in a using statement must be implicitly convertible to System.IDisposable
If instead I do it in VB.NET everything goes smooth:
<pre class="prettyprint lang-vb Dim productServices As IProductServices

Using productServices As New ProductServices
some code
End Using[/code]
Of course if I change the interface code in public interface IProductServices : IDisposable, the error goes away. However it looks to me really redundant to specify the implementation of IDisposable in both interface and class. Is there other way to
solve this problem?<br/>
<br/>






View the full article
 
Back
Top