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

Home Posts Topics Members FAQ

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(<som e 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 2038
On Sep 4, 1:38 pm, "Jure Bogataj" <jure.boga...@m ikrocop.comwrot e:
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**********@m ikrocop.comwrot e in message
news:uz******** ******@TK2MSFTN GP02.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(<som e 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...@m ikrocop.comwrot e:
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
2956
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 that is after the newly added row without getting bizarre results. I have added the full code for the test below. Create a project drop in the code and run. There is nothing crazy about the code. I used the designer to add the dataset and to...
1
8006
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 under 2002) Create a form and add two buttons (bottom right) and a panel (fill the top of the form) Anchor the buttons to the bottom right and anchor the panel to top bottom left and right. set all three controls access level to protected
3
25280
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 user control to invoke a method in the parent form. This code doesn't seem to work for me, I've tried it a number of times with very simple test cases (A user control with a single button and a parent form with a single text box) and it always...
9
3966
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 will not set the value of the control and will leave the checked status of the checkbox to false when a user selects a new date. this works fine when using the control on a win2k machine but if we use it on a win XP box and call
3
5306
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 have implemented Clone() using serialization. When I call the Clone() function, the following exception is thrown at
0
2662
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 my Completion ComboBox using sendkey.wait. From the keyboard (that will not exist for my final application) I can enter text into my Completion and the selection completes as expected.
14
5793
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 SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do tell those to me too coz I have no idea how to go about doing it.
0
2050
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 : axWebBrowser2.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,ref empty,ref empty);
6
6135
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? I tried using javascript, however, I am not able to make javascript wait for the page to completely load, before trying to access and fill fields on the page. Hence resorted to .net, but not finding the right library to use.
2
2643
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, it should be created with a using statement. If it's a member of a class, that class should have a Dispose function or call Dispose on the object somewhere in there.
0
8685
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
9171
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
9032
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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
8880
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...
1
6532
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
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.