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(StreamReader 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 3 2125
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(StreamReader 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 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.
Thanks y'all.
"Razzie" <ra****@quicknet.nl> wrote in message
news:O3****************@TK2MSFTNGP09.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(StreamReader 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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
}
|
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...
|
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....
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |