472,981 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 software developers and data experts.

The using statement vs the try-catch-finally

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

Sep 10 '08 #1
5 7063
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" wrote:
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

Sep 11 '08 #2
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" <KH@discussions.microsoft.comwrote in message
news:E5**********************************@microsof t.com...
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" wrote:
>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

Sep 11 '08 #3
Oh never mind my last question. I rethought how try-finally is in use.

"KH" <KH@discussions.microsoft.comwrote in message
news:E5**********************************@microsof t.com...
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" wrote:
>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

Sep 11 '08 #4
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)
{
Sep 11 '08 #5
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" <M8********@mailinator.comwrote in message
news:sd******************************@comcast.com. ..
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" <KH@discussions.microsoft.comwrote in message
news:E5**********************************@microsof t.com...
>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" wrote:
>>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

Sep 11 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bill Priess | last post by:
Hey gang, Ok, I'm stumped on this one... I am using the using statement to wrap a SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope...
1
by: Kepler | last post by:
I have a situation where I need to use a Using statement that creates some records in a database. After that completes, if it completes, I need to do some file creation. Any code I'm putting...
3
by: SD WinGirl | last post by:
I am writing an automation utility with the DTE model in C#. I need to open up a C# file and read all the "using statements" contained in that file. I do not see a way to be able to do this with...
4
by: David Parker | last post by:
The below seems to work... using(dbconn=DatabaseHelper.CreateConnection("David_Projects")) { using(dbcmd=GetSQLSearchCommand(dbconn)) { // code here } }
8
by: J-T | last post by:
I have a class like below I have a couple of questions about that: 1) I like to use "Using statement" when creating an object of this class,so I had to implement IDisposable.Am I doing this right...
19
by: Arjen | last post by:
Hi, Inside my method I'm using the using-statement for data access. Before that, I create an object of a class. This works fine. Inside the using-statement I set properties of the class (with...
18
by: Trevor | last post by:
I have seen the "using" statement used in strange ways in some C# code. Example: using (StreamReader sr = new StreamReader("TestFile.txt")) { // do some stuff... } What effect does the...
6
by: Juan Zamudio | last post by:
Hi all, I'm using vs 2003, i have a question about using and as, i was reading in programing .net components that one way to avoid null references inside using was using as, something like this:...
23
by: Tony Johansson | last post by:
Hello! I just wonder what is the point of having the reader variable declared as TextReader in the snippet below.. Is it because of using the polymorfism on the reader variable perhaps. using...
2
by: mcfly1204 | last post by:
What is the best practice for returning values from within a using statement? For instance: using (SqlConnection Conn = new SqlConnection(strConn)) { ...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.