The using statement vs the try-catch-finally

  • Thread starter Thread starter Hillbilly
  • Start date Start date
H

Hillbilly

MSDN Remarks [1] "as a rule" the using statement should be used when
instantiating objects which inherit IDisposable. Other than the obvious
unmanaged objects like the file system example, fonts and the database how
else may it be determined which classes and their objects inherit
IDisposable?

And the remarks imply the using statement is a different and perhaps more
efficient way to use objects than try-catch-finally?

[1] http://msdn.microsoft.com/en-us/library/yh598w02.aspx
 
Use the documentation - MSDN - to see if a type implements IDisposable - it
will be in the type signature, e.g:

public class SomeType : IDisposable;

Or you could type some code in VS and see if Dispose() is in the
intellisense list.

The using statement compiles to a try/finally block under the covers so I
doubt it's any more efficient than writing the try/finally yourself.

HTH
 
I see what you mean however it implies the use of a try-catch within the
using code block anyway as where do the exceptions then get caught and
managed?


KH said:
Use the documentation - MSDN - to see if a type implements IDisposable -
it
will be in the type signature, e.g:

public class SomeType : IDisposable;

Or you could type some code in VS and see if Dispose() is in the
intellisense list.

The using statement compiles to a try/finally block under the covers so I
doubt it's any more efficient than writing the try/finally yourself.

HTH


Hillbilly said:
MSDN Remarks [1] "as a rule" the using statement should be used when
instantiating objects which inherit IDisposable. Other than the obvious
unmanaged objects like the file system example, fonts and the database
how
else may it be determined which classes and their objects inherit
IDisposable?

And the remarks imply the using statement is a different and perhaps more
efficient way to use objects than try-catch-finally?

[1] http://msdn.microsoft.com/en-us/library/yh598w02.aspx
 
Oh never mind my last question. I rethought how try-finally is in use.

KH said:
Use the documentation - MSDN - to see if a type implements IDisposable -
it
will be in the type signature, e.g:

public class SomeType : IDisposable;

Or you could type some code in VS and see if Dispose() is in the
intellisense list.

The using statement compiles to a try/finally block under the covers so I
doubt it's any more efficient than writing the try/finally yourself.

HTH


Hillbilly said:
MSDN Remarks [1] "as a rule" the using statement should be used when
instantiating objects which inherit IDisposable. Other than the obvious
unmanaged objects like the file system example, fonts and the database
how
else may it be determined which classes and their objects inherit
IDisposable?

And the remarks imply the using statement is a different and perhaps more
efficient way to use objects than try-catch-finally?

[1] http://msdn.microsoft.com/en-us/library/yh598w02.aspx
 
The slap upside the head to remember to read my Intellisense was what I
needed but I thought I read while searching the web that one intended use of
the using statement will dispose automatically as an intentional response to
such circumstance such as the example you mention. And are you using the
term function to refer to a method of a class?

Mark Tolonen said:
If you are unsure that a class implements IDisposable (perhaps the class
name was passed to a function and created dynamically), the construction:

using(instance as IDisposable)
{
.
.
.
}

will call Dispose on instance only if the method exists.

-Mark


KH said:
Use the documentation - MSDN - to see if a type implements IDisposable -
it
will be in the type signature, e.g:

public class SomeType : IDisposable;

Or you could type some code in VS and see if Dispose() is in the
intellisense list.

The using statement compiles to a try/finally block under the covers so I
doubt it's any more efficient than writing the try/finally yourself.

HTH


Hillbilly said:
MSDN Remarks [1] "as a rule" the using statement should be used when
instantiating objects which inherit IDisposable. Other than the obvious
unmanaged objects like the file system example, fonts and the database
how
else may it be determined which classes and their objects inherit
IDisposable?

And the remarks imply the using statement is a different and perhaps
more
efficient way to use objects than try-catch-finally?

[1] http://msdn.microsoft.com/en-us/library/yh598w02.aspx
 
Back
Top