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

Home Posts Topics Members FAQ

Using keyword and IDisposable

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 and using{} might be an extra safeguard.

Any help with this would be appreciated.

Paul
Oct 7 '08 #1
3 1346
idisposable isn't required unless you have special circumstances for
cleaning up objects. Strings are handled automatically. Setting a string to
empty or null does nothing for you and in fact prolongs the life of the
string by the extra references. If you intend your data layer to collect
unmanaged objects at some point in its lifetime or requires care in
destroying the objects, definitely implement idisposable. I understand some
people simply like to implement idisposable because it is recommended. I
think that's ok too as long as you understand its impact on your code.

--
Regards,
Alvin Bruney

Want a free copy of VS 2008 w/ MSDN premium subscription?
Details at http://msmvps.com/blogs/alvin/Default.aspx

Auther Plug
OWC Blackbook now on download at www.lulu.com/owc

"Paul" <Ge**********@g mail.comwrote in message
news:78******** *************** ***********@z72 g2000hsb.google groups.com...
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 and using{} might be an extra safeguard.

Any help with this would be appreciated.

Paul
Oct 7 '08 #2
On 7 Oct, 12:09, "Alvin Bruney [ASP.NET MVP]" <vapor dan a t h u t ma
le dut cu mwrote:
idisposable isn't required unless you have special circumstances for
cleaning up objects. Strings are handled automatically. Setting a string to
empty or null does nothing for you and in fact prolongs the life of the
string by the extra references. If you intend your data layer to collect
unmanaged objects at some point in its lifetime or requires care in
destroying the objects, definitely implement idisposable. I understand some
people simply like to implement idisposable because it is recommended. I
think that's ok too as long as you understand its impact on your code.

--
Regards,
Alvin Bruney

Want a free copy of VS 2008 w/ MSDN premium subscription?
Details athttp://msmvps.com/blogs/alvin/Default.aspx

Auther Plug
OWC Blackbook now on download atwww.lulu.com/owc

"Paul" <Gef.Mongo...@g mail.comwrote in message

news:78******** *************** ***********@z72 g2000hsb.google groups.com...
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 and using{} might be an extra safeguard.
Any help with this would be appreciated.
Paul- Hide quoted text -

- Show quoted text -
Thanks for the reply Alvin. The datalayer seems to do a pretty good
job of closing connections and cleaning up after itself (I haven't had
any issues yet), but recently I read a blog regarding someone
implimenting IDisposable in their DB layer and seeing memory usage
reduced by up to 40% - so thought i'd look into it. Does implementing
it have a performance hit? If so, i'll just stick to the way I was
doing my clean up before.

Thanks for the help.
Oct 7 '08 #3
a class implements idisposable if it uses unmanged resources, and wants them
released as soon as possible, not when the GC get around to it. if a class
implements idisposable it should always be called. better yet use a using
statement. database connections, and commands need to be disposed. if your
code (as it should) always disposes these object before returning from any
method, then your class does not need to implement idispose.

there is a cost to calling idispose, as it forces a GC run on the current
thread rather than the background thread.

List<does not implement IDispose as it does not need it. but if you insert
objects that require disposing, your code needs to loop thru the list and
dispose the objects when you are done with the list<>

-- bruce (sqlwork.com)
"Paul" wrote:
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 and using{} might be an extra safeguard.

Any help with this would be appreciated.

Paul
Oct 7 '08 #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...
7
3020
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the "Close()" methods reared their ugly (at least it would seem...)heads. I was happily delving away in the .NET framework, investigating the stream classes with the msdn and Lutz Roeder's .NET reflector, when I stumbled upon the following:
3
2143
by: Razzie | last post by:
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() {
5
5610
by: Andreas Müller | last post by:
Hi, I was wondering, if there is something similar in VB.NET like the using statement in C#. What it does is to automatically call Dispose on the object decrared with in the statement when the block exits. using (MyBoj) { }// MyObj.Dispose is called
3
3492
by: Dave | last post by:
I trying to determine the best pattern for designing my business and data layers... Can the instance of the business object eventually cause memory leaks in Example 1? If your business class doesn't implement IDisposable and falls out scope, does it eventually get cleaned up by the GC or should it be set to NULL? If it can cause leaks, should the IDisposable always be implemented at a base class level as a best practice?
2
1251
by: RKNET | last post by:
Please let me know on the usage of 'Using' keyword when used in statments. Eg; using (System.IO.StreamWriter varFile = new System.IO.StreamWriter("fileName")). Thanks, RKNET
2
1510
by: R.A.M. | last post by:
Hello, I have started larning C# and I have a question concerning "using (...)" keyword. For example: using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); .... connection.Close();
10
1976
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL, CN) Dim DR As OleDbDataReader = CMD.ExecuteReader()
4
1079
by: Shahil Shah | last post by:
Hello I would like to use the 'using' keyword (available in c#) in visual basic. I have looked at the MSDN library but it does nat have this in vb. Does anybody know of a similar keyword/function in vb? Thanks Shahil
0
9711
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
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
10343
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...
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
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.