1

The following doesn't work:

    public string foo()
    {
        using (Random myRandomChoice = new Random())
        {
            return myRandomChoice.Next(10).ToString();
        }
    }

The following is errors:

    public string foo()
    {
        Random myRandomChoice = new Random();
        return myRandomChoice.Next(10).ToString();
    }

The error message for the first foo is concerning Random not being implicitly convertable to System.IDisposable.

Is this a deliberate ploy withing the syntax of the language so you only use using in specific circumstances e.g when dealing with database connections? or can I explicitly convert Random to type IDisposable so that the initial foo works?

Is there a list available of types that would be better declared with using ?

whytheq
  • 34,466
  • 65
  • 172
  • 267
  • The goal of IDisposable is not to be compatible with using, but is to help to manage unmanaged resources (File handles, connections to DB, etC.). Using is only a syntax sugar to simplify the call to the Dispose method. Do you mean that you use using to reduce scope of variable? – Steve B Jul 11 '12 at 07:32
  • Any type that implements IDisposable can be used in a using statement. See my answer for how to find those types. – Ergwun Jul 11 '12 at 07:49
  • and http://stackoverflow.com/questions/567138/when-should-i-use-using-blocks-in-c and http://stackoverflow.com/questions/1033334/is-there-a-list-of-common-object-that-implement-idisposable-for-the-using-statem (this one is rather neat as it gives you a way to list all classes that implement IDisposable) – dash Jul 11 '12 at 07:53

4 Answers4

4

See using C#

The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

Since Random does not implement System.IDisposable, that is why you get the error.

From MSDN

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

Habib
  • 219,104
  • 29
  • 407
  • 436
1

using is possible to execute on types that implement IDisposable.

In this way implemented Dispose(..) method on that type will be executed as soon as execution of the program jumps out of using block.

it's nothing else then "alias" for try/finally construct, injected by compiler in IL

Tigran
  • 61,654
  • 8
  • 86
  • 123
1

using is a better syntactic way of calling the dispose on IDisposable ( probably in a try-finally):

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block;

http://msdn.microsoft.com/en-us/library/yh598w02.aspx

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • ok - so similar result to `try-finally`. But my question is that `using` can't always be the correct structure to use as not _all_ types work with it. Is there a list of the types which are good with `using`? – whytheq Jul 11 '12 at 07:44
0

A using statement is a way to guarantee that Dispose is called on an object even when an exception is thrown. It is therefore meaningless when the object does not implement IDisposable.

The following code examples are equivalent:

(1) A using statement:

using (MyClass myObj = new MyClass())
{
    ...
}

(2) Disposing in a finally block:

try
{
    MyClass myObj = new MyClass();
    ...
}
finally
{
    myObj.Dispose();
}

As you can see, all the using statement achieves is disposal of the object, therefore it makes no sense if you object doesn't implement IDisposable.

FXCop has a rule for checking that you have used using statements for your IDisposable types. See this StackOverflow question for other ways to find which types implement IDisposable and can be used in a using statement.

See the MSDN documentation for more information.

Community
  • 1
  • 1
Ergwun
  • 12,579
  • 7
  • 56
  • 83