By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,881 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,881 IT Pros & Developers. It's quick & easy.

using disposing object

P: n/a
Hello!

I'm using .NET 2005 (C#) and I've come across using statement. It seems ok,
I was just wondering one thing. What if I returned object inside using
statement using RETURN directive. Is returned object valid or not?

For example:

public FileStream Func1()
{
...
using (FileStream fs = new FileStream(<some params>))
{
// Do sth with FS
return fs;
}
}

is on return from Func1 FileStream object returned valid, since it is called
from inside using statement? Does "using" statement *always* call Dispose,
or does it just decrement object's reference? If its the first (called
Dispose), than I must not use using statement for that kind of operations or
on variables I must return?

Thanks in advance!

Best regards,
Jure
Sep 4 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sep 4, 1:38 pm, "Jure Bogataj" <jure.boga...@mikrocop.comwrote:
I'm using .NET 2005 (C#) and I've come across using statement. It seems ok,
I was just wondering one thing. What if I returned object inside using
statement using RETURN directive. Is returned object valid or not?
That depends on what Dispose does for that types. For some types, you
can still do some things with them. (A MemoryStream will still let you
get at its data with ToArray, IIRC).
Does "using" statement *always* call Dispose,
or does it just decrement object's reference?
There's no reference counting in .NET, and Dispose is very separate
from garbage collection. Dispose is just another method as far as the
framework is concerned - the IDisposable interface is a convention
rather than something enforced, although it has language support in C#
and C++/CLI.
If its the first (called
Dispose), than I must not use using statement for that kind of operations or
on variables I must return?
Yes, basically.

Jon

Sep 4 '07 #2

P: n/a
IMO, Dispose() method will definitely be called if you created an object
inside 'using' statement, even if the object is returned as the function
return value. So, you'd better not return objects that are declared in
'using' statements.

"Jure Bogataj" <ju**********@mikrocop.comwrote in message
news:uz**************@TK2MSFTNGP02.phx.gbl...
Hello!

I'm using .NET 2005 (C#) and I've come across using statement. It seems
ok, I was just wondering one thing. What if I returned object inside using
statement using RETURN directive. Is returned object valid or not?

For example:

public FileStream Func1()
{
...
using (FileStream fs = new FileStream(<some params>))
{
// Do sth with FS
return fs;
}
}

is on return from Func1 FileStream object returned valid, since it is
called from inside using statement? Does "using" statement *always* call
Dispose, or does it just decrement object's reference? If its the first
(called Dispose), than I must not use using statement for that kind of
operations or on variables I must return?

Thanks in advance!

Best regards,
Jure


Sep 4 '07 #3

P: n/a
>Does "using" statement *always* call Dispose,
>or does it just decrement object's reference?

There's no reference counting in .NET, and Dispose is very separate
from garbage collection. Dispose is just another method as far as the
framework is concerned - the IDisposable interface is a convention
rather than something enforced, although it has language support in C#
and C++/CLI.
How come there's no reference counting? Isn't reference counting something
that GC depends on for destroying objects (e.g. knowing when object can be
safely destroyed; meaning there exists no reference to it whatsoever).
And Dispose() is also used with GC is it not?

Some lightshed on this topic would be greatly appreciated!

Best regards,
Jure
Sep 4 '07 #4

P: n/a
On Sep 4, 2:10 pm, "Jure Bogataj" <jure.boga...@mikrocop.comwrote:
There's no reference counting in .NET, and Dispose is very separate
from garbage collection. Dispose is just another method as far as the
framework is concerned - the IDisposable interface is a convention
rather than something enforced, although it has language support in C#
and C++/CLI.

How come there's no reference counting? Isn't reference counting something
that GC depends on for destroying objects (e.g. knowing when object can be
safely destroyed; meaning there exists no reference to it whatsoever).
No, the garbage collector uses a mark and sweep algorithm for finding
live objects, not reference counting. Reference counting generally
breaks in the face of cyclic references etc, as well as having
performance issues.
And Dispose() is also used with GC is it not?
No, it's not.
Some lightshed on this topic would be greatly appreciated!
See http://msdn.microsoft.com/msdnmag/is...t/default.aspx
for a bit more information. (I haven't read it all to check its
correctness, but it looks okay.)

Jon

Sep 4 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.