I like the using () {} statement for its control over the scope and for readability. You can not only create objects, use them and carefully dispose of them, but you can also use them like this:
Suppose myInstance is an instance of MyClass from another place in the code - that is, a method parameter or something
using (var t = myInstance) {
t.Foo= "Hello";
t.Bar= "World";
...
}
MyClass definition:
public class MyClass : IDisposable
{
public string Foo {get; set;}
public string Bar {get; set;}
...
public void Dispose() {}
}
This makes the code tidier and easier to read (I feel) and of course easier to type. However, to use this widely, you need to implement IDisposable. In this case, there is no need for MyClass to actually process anything in Dispose, so its empty.
My question is, is there a drawback to using IDisposable this way?
, , , IDispose ( ).
EDIT:
, , , . using - , , . , . , , , null, , . ?