473,804 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using clause and implementing IDisposable

Hi,

I know that as a general rule, whenever your class contains members that
implicitly or explicitly implement IDisposable, your class should too.
However, does it count when my class uses a using claus?

class A
{
private void doSomething()
{
using(StreamRea der sr = new StreamReader(.. ))
{ // code }
}
}

1. Would class A still need to implement IDisposable, or is the using clause
completely 'safe'?
2. If I use a thy-catch clause around the using clause, and an exception
occurs, will some resources (the StreamReader) not get cleaned up properly?
(And maybe that would be the answer to number 1, eg. Yes I should still
implement IDisposable?)
3. When using the using clause, would I still need to call sr.Close()? I
take it the Close() method eventually calls the Dispose() method too, so it
wouldn't be necessary...

Thank you!

Razzie
Nov 16 '05 #1
3 2143
Razzie wrote:
Hi,

I know that as a general rule, whenever your class contains members
that implicitly or explicitly implement IDisposable, your class
should too. However, does it count when my class uses a using claus?

class A
{
private void doSomething()
{
using(StreamRea der sr = new StreamReader(.. ))
{ // code }
}
}

1. Would class A still need to implement IDisposable, or is the using
clause completely 'safe'?
It's safe. No need to implement IDisposable in your class (it wouldn't help
anyway).
2. If I use a thy-catch clause around the using clause, and an
exception occurs, will some resources (the StreamReader) not get
cleaned up properly? (And maybe that would be the answer to number 1,
eg. Yes I should still implement IDisposable?)
"Using" is a syntactical shortcut for a try/finally block that invokes
Dispose() on the "used" IDisposable. Thus, it acts like a nested try block.
3. When using the using clause, would I still need to call
sr.Close()? I take it the Close() method eventually calls the
Dispose() method too, so it wouldn't be necessary...


Close() is (or should be) a synonym for Dispose() for types that support the
notion of closing, like readers, streams, or windows. In your example, "sr"
will be closed and thus also disposed.

Cheers,

--
Joerg Jooss
www.joergjooss.de
ne**@joergjooss .de
Nov 16 '05 #2
1. Would class A still need to implement IDisposable
No.

, or is the using clause completely 'safe'?
Yes.
2. If I use a thy-catch clause around the using clause, and an exception
occurs, will some resources (the StreamReader) not get cleaned up
properly?
It will get cleaned up fine.
3. When using the using clause, would I still need to call sr.Close()? I
take it the Close() method eventually calls the Dispose() method too, so
it wouldn't be necessary...


As far as I know, Dispose() normally calls Close() as a matter of course.
Nov 16 '05 #3
Thanks y'all.

"Razzie" <ra****@quickne t.nl> wrote in message
news:O3******** ********@TK2MSF TNGP09.phx.gbl. ..
Hi,

I know that as a general rule, whenever your class contains members that
implicitly or explicitly implement IDisposable, your class should too.
However, does it count when my class uses a using claus?

class A
{
private void doSomething()
{
using(StreamRea der sr = new StreamReader(.. ))
{ // code }
}
}

1. Would class A still need to implement IDisposable, or is the using
clause completely 'safe'?
2. If I use a thy-catch clause around the using clause, and an exception
occurs, will some resources (the StreamReader) not get cleaned up
properly? (And maybe that would be the answer to number 1, eg. Yes I
should still implement IDisposable?)
3. When using the using clause, would I still need to call sr.Close()? I
take it the Close() method eventually calls the Dispose() method too, so
it wouldn't be necessary...

Thank you!

Razzie

Nov 16 '05 #4

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

Similar topics

26
448
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of scope, the dtor could be immediately be called (no GC needed). For that, you don't have to declare the whole File class as a struct (which would be not good for performance when File has a lot of data-members). Instead creating a thin wrapper...
3
4508
by: Ivan Neganov | last post by:
Hi, I have a custom subclass of System.IO.Stream type. I wonder how to correctly implement the IDisposable pattern in this situation. The parent Stream type apparently uses explicit interface implementation, and I could not find a way for my child type to override parent's IDisposable.Dispose() method. The intention was to clean up child's resources first,
6
1943
by: newbie | last post by:
I have Class A that implements IDisposable, within this class i have method A that creates a new Sqlconnection object and execute some stored proc, in Class B, I create an instance of Class A object with the using statement.. my question is when my using statement ends Class A's dipose method should be called i think.. but will my Sqlconnection object within Class A Method A be disposed automatically as well?
2
1702
by: Andrew Quine | last post by:
Hi Short one: looking for a good example and an explanation of such enumerators. I know MessageEnumerator and ResourceReader do this, for example, but what is the implementation of the enumerator doing that needs a Dispose of its own? Cheers! Andrew Quine
2
1119
by: Per Rollvang | last post by:
Hi all! I have created a few classes that inherits the IDisposable-interface, and if I don't use them in this fashion using(bla bla bla) { // do stuff }
8
3708
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...
2
5020
by: Robert Bravery | last post by:
Hi all, Being new to C# and .net I often don't know how to use things. I have created an app that imports excel data, it works well, with methods to open excel, extract the data and close excel. Now a colege of mine suggested useinf the usingstatement, so that the office object is dispossed/destroyued correctly, saying that it prevents memoryleaks. Problem is I am having problems trying to achieve this, I keep getting the error: type...
7
1802
by: =?Utf-8?B?RnJhbnM=?= | last post by:
Hi, I'm working on an application where it is essential to free underlying non-memory resources as soon as they are no longer needed. In my case, it is a VISA resource which is used in automated tests (involving instruments), but my question is relevant for any other resource, like those held by a FileStream. The problem that I'm facing is that this resource is used, either directly or indirectly, by different objects. Let me rephrase...
3
1346
by: Paul | last post by:
Hi all, I currenty have a datalayer and have decided to impliment IDisposable with it. The data layer contains a number of objects which I can call dispose on, but i'm not too sure if you need to worry about strings. In the dispose method, is it best to set these to "", or set them to null? Also, I have an List<Tobject, should this be cleared and set to null? I usually clean things up myself, but figured for a datalayer, IDisposable...
0
9591
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
10594
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...
0
10087
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
9166
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...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
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...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
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
3831
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.