473,322 Members | 1,538 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,322 software developers and data experts.

using disposing object

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
4 2013
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
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave | last post by:
I tried posting this in the WinForm forum and got no hits so I am trying it here. After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row...
1
by: Jason Hickey | last post by:
Has there been a change in the way the UI designer handles winform inheritance in the 2003 version of visual studio. Consider the following (try it if you are using 2003 Everything seems to work...
3
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a...
9
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it...
3
by: scoobydoo | last post by:
Hello, I am trying to implement ICloneable's Clone() function, using Serialization. However, my code causes an exception. I have a class derived from TreeNode called "Node1". In Node1, I...
0
by: Hiroyuki Tanaka | last post by:
Hi All, I am trying to develop an application for a touch screen using buttons for the numeric pad with Completion ComboBoxes. At the moment I am having a problem sending the button presses to...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
0
by: sagar.jawale | last post by:
Hi, In my c# windows application, i am using AxSHDocVw.AxWebBrowser. I am displaying a generated receipt html in this browser. Also, for printing the same html, i am using the following command...
6
by: =?Utf-8?B?U2FtZWVrc2hh?= | last post by:
Hi, I want to write a simple .net program to open a URL, fill in fields, and click on a button to submit it using .net 1.1 framework. Can someone help in suggesting the libraries I should use?...
2
by: not_a_commie | last post by:
Are there any tools available to track down IDisposable objects that don't have a call to Dispose? I'm thinking this can be done with static code analysis. If the object is created inside a method,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.