I am use using to guarantee the resource cleanup. This is fine for the simple block of code.
If I have a method which I need to pass the variable inside the using, can I still guarantee resource clean up?
for example,
using ( FileStream fs = -----)
{
SomeMethod(fs);
}
Yes.
using(){}is syntactic sugar, and it will expand your code to this:finallyblocks are guaranteed to execute whether an exception is thrown or not. The only time they wouldn’t be executed is in the case of a severe runtime failure, such as a stack overflow, out of memory exception, or a crash in the runtime itself.