473,698 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7136
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.comw rote in message
news:E5******** *************** ***********@mic rosoft.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
instantiatin g 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.comw rote in message
news:E5******** *************** ***********@mic rosoft.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
instantiatin g 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********@mai linator.comwrot e 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.comw rote in message
news:E5******** *************** ***********@mic rosoft.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
instantiati ng 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
2702
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 applies to objects created in the using scope. For example: <code> static DataTable getTable()
1
315
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 after the Using statement gives a compiler warning of "Unreachable Code Detected", and it never runs. Why is this happening? I really don't want to put the file manipulation code in the using statement because it's a transaction. I also need for...
3
1930
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 the ProjectItem. Have I missed something? Can someone please suggest a way to accomplish this? Thanks.
4
1735
by: David Parker | last post by:
The below seems to work... using(dbconn=DatabaseHelper.CreateConnection("David_Projects")) { using(dbcmd=GetSQLSearchCommand(dbconn)) { // code here } }
8
3703
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 here? 2) Do I have to be worried about those objects I have created in GetPackageNames() ?? for instance making them null afterward or something? 3) This class is in my business layer As you can see I'm using Microsoft.SQLServer.DTSPkg80...
19
3979
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 the data from the database). Getting the data works. Setting the data doesn't work. It say's:
18
2362
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 using statement give you in this example?
6
1954
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: using(var as IDisposable){ ..... } the book saids that if the conversion is null the code inside using will not run, but i cant make it work, I have a FileStream that opens a file, the path is a parameter, something like this:
23
1719
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 (TextReader reader = new StreamReader(fullPathname)) { string line; while ((line = reader.ReadLine()) != null)
2
1271
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)) { SqlCommand cmd = new SqlCommand("spWhatever", Conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@parm1", SqlDbType.VarChar, 128); cmd.Parameters.Value = strOne; ...
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.