473,395 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 7101
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)) { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.