Friday, June 5, 2009

'System.Collections.IEnumerator': type used in a using statement must be implicitly convertible to 'System.IDisposable'

Scenario:
While executing code below I got the following error

Error :'System.Collections.IEnumerator': type used in a using statement must be implicitly convertible to 'System.IDisposable'

Code with Error :

using(IEnumerator enumerator = base.internalDataSource.GetEnumerator())
{

// Execute the code here

}
Solution:
The 'using' statement is, as the message states, closely coupled with IDisposable. For this to work, 'MyType' must implement IDisposable, which is an interface wich defines the member 'void Dispose()'; If MyType does not implement IDisposable, t.Dispose() can't be called. That's why you're getting the message.

1. Implement the interface
2. Correct the code as shown below with no disposing

Correct Code:
{
IEnumerator enumerator = base.internalDataSource.GetEnumerator();

// Execute the code here

}


Article:
Follow on Twitter

0 comments: