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

IDisposable

Hi,

Here I have made some sample code.
I want to know if I'm using IDisposable on the right way.

public class Person {
public string name;
public string address;
}

public class main {

try {
Person person = new Person();
person.name = "Arjen";
}
catch{
throw;
}
finally{
((IDisposable)person).Dispose();
}

}

Is the IDisposable useful here?
(I want that the object don't exist any more in the memory)

Thanks!
Nov 17 '05 #1
4 2135
Arjen,

IDisposable is used to indicate when an instance should release some
resource that is expensive. Almost all of the time, this is in reference to
something like a file handle, a socket, a database connection, or something
like that. It doesn't do anything to indicate that the object will exist
anymore in memory.

You can't control when the object is released from memory, that's what
the GC is for.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arjen" <bo*****@hotmail.com> wrote in message
news:dd**********@news4.zwoll1.ov.home.nl...
Hi,

Here I have made some sample code.
I want to know if I'm using IDisposable on the right way.

public class Person {
public string name;
public string address;
}

public class main {

try {
Person person = new Person();
person.name = "Arjen";
}
catch{
throw;
}
finally{
((IDisposable)person).Dispose();
}

}

Is the IDisposable useful here?
(I want that the object don't exist any more in the memory)

Thanks!

Nov 17 '05 #2
KH
Since class Person does not implement IDisposable the cast will fail at
runtime. It is not useful though, because you are not using any unmanaged
resources, streams, or the like. Implement idisposable for classes that use
things like COM calls, database connections, file streams...
"Arjen" wrote:
Hi,

Here I have made some sample code.
I want to know if I'm using IDisposable on the right way.

public class Person {
public string name;
public string address;
}

public class main {

try {
Person person = new Person();
person.name = "Arjen";
}
catch{
throw;
}
finally{
((IDisposable)person).Dispose();
}

}

Is the IDisposable useful here?
(I want that the object don't exist any more in the memory)

Thanks!

Nov 17 '05 #3
"Arjen" <bo*****@hotmail.com> wrote in message
news:dd**********@news4.zwoll1.ov.home.nl...
Is the IDisposable useful here?


No, because it's not using unmaged code.
Nov 17 '05 #4
Okay, thank you all.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schreef
in bericht news:OL**************@TK2MSFTNGP09.phx.gbl...
Arjen,

IDisposable is used to indicate when an instance should release some
resource that is expensive. Almost all of the time, this is in reference
to something like a file handle, a socket, a database connection, or
something like that. It doesn't do anything to indicate that the object
will exist anymore in memory.

You can't control when the object is released from memory, that's what
the GC is for.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arjen" <bo*****@hotmail.com> wrote in message
news:dd**********@news4.zwoll1.ov.home.nl...
Hi,

Here I have made some sample code.
I want to know if I'm using IDisposable on the right way.

public class Person {
public string name;
public string address;
}

public class main {

try {
Person person = new Person();
person.name = "Arjen";
}
catch{
throw;
}
finally{
((IDisposable)person).Dispose();
}

}

Is the IDisposable useful here?
(I want that the object don't exist any more in the memory)

Thanks!


Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Samuel R. Neff | last post by:
I'd like some opinions on whether or not to use IDisposable for classes that require clean-up but when the clean-up is not related to unmanaged resources or other disposable objects. The most...
5
by: Robert Heuvel | last post by:
Hi, this is what I did: public struct SWaitCursor:IDisposable { public SWaitCursor (int i) { Cursor.Current = Cursors.WaitCursor; } void System.IDisposable.Dispose() { Cursor.Current =...
3
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...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
6
by: Water Cooler v2 | last post by:
I heard from someone that we must not implement IDisposable for all classes. Can someone please tell me: 1. the reason why we must not implement IDisposable for all the classes we write. 2....
12
by: Cordell Lawrence \(News Group\) | last post by:
There an ongoing discussion between a colleague and myself about the usefulness of the IDisposable pattern beyond the reclamation of unmanaged resources. The discussion is somewhat lengthy so I...
12
by: Mark Rae | last post by:
Hi, The following code works: HttpWebRequest objRequest = null; try { HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com"); using (HttpWebResponse...
11
by: Mark Rae | last post by:
Hi, Following on from the recent thread about why HttpWebRequest doesn't implement the IDisposable interface, I got to wondering whether people make their custom classes inherit IDisposable or...
3
by: Mark | last post by:
If your class implements IDisposable, I was told that this increases the speed with which your class is garbage collected. Is this true? And if so, how much "time" does it save? Assume that we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.