Compiler Error CS1674

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

The using statement is intended to be used to ensure the disposal of an object at the end of the using block, thus, only types which are disposable may be used in such a statement. For example, value types are not disposable, and type parameters which are not constrained to be classes may not be assumed to be disposable.

Example 1

The following sample generates CS1674.

// CS1674.cs  
class C  
{  
   public static void Main()  
   {  
      int a = 0;  
      a++;  
  
      using (a) {}   // CS1674  
   }  
}  

Example 2

The following sample generates CS1674.

// CS1674_b.cs  
using System;  
class C {  
   public void Test() {  
      using (C c = new C()) {}   // CS1674  
   }  
}  
  
// OK  
class D : IDisposable {  
   void IDisposable.Dispose() {}  
   public void Dispose() {}  
  
   public static void Main() {  
      using (D d = new D()) {}  
   }  
}  

Example 3

The following case illustrates the need for a class type constraint to guarantee that an unknown type parameter is disposable. The following sample generates CS1674.

// CS1674_c.cs  
// compile with: /target:library  
using System;  
public class C<T>  
// Add a class type constraint that specifies a disposable class.  
// Uncomment the following line to resolve.  
// public class C<T> where T : IDisposable  
{  
   public void F(T t)  
   {  
      using (t) {}   // CS1674  
   }  
}