The using statement vs the try-catch-finally

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
 
K

KH

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
 
H

Hillbilly

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
 
H

Hillbilly

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
 
H

Hillbilly

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top