9

Does this make sense ?

Having a simple class

 class Test { public Test() { // Do whatever } } 

and then instantiating it

using(new Test()) { // Nothing in here } 

The using statement is there just to make sure Test is disposed. When will it be disposed if I just call

new Test() 
4
  • 10
    Is this under the assumption that Test implements IDisposable? Commented Aug 4, 2010 at 7:36
  • No it doesn't. How to know if a class needs to implement it or not ? Do you always do it ? Commented Aug 4, 2010 at 7:39
  • 3
    I suspect that you are confusing "gargage collection" and "disposal", which are two related but very different concepts. Commented Aug 4, 2010 at 7:40
  • I think he's probably used to C++ or the like where you'd need to delete an object after new'ing it. Have a read about garbage collection. Commented Aug 4, 2010 at 9:03

3 Answers 3

11

Test isn't IDisposable, so that won't even compile. But if it was disposable, yes using will do it, and new by itself won't. It is an unusual usage, but I've seen similar. Rarely.

Based on your comment (main question), I suspect that you are confusing garbage collection and disposal. There is no way to forcibly make something get collected, short of GC (which you should not do). Unless you have a really good reason to want it collected, just let it be - chances are it is "generation 0" and will be collected cheaply anyway.

Also, the "do whatever" suggests doing something in a constructor but not caring about the created object; a static method would be preferable:

public class Test { /* could be static */ public static void DoSomething() { ... } } ... Test.DoSomething(); 
Sign up to request clarification or add additional context in comments.

6 Comments

I'd argue on the 'unusual usage', in fact, I use it quite often.
@Yossarian - can you please explain why, and how is it better than a normal method?
well : some (initial) action is binded with another (final) action, for example: HtmlWriter.RenderBeginTag(), RenderEndTag(). If you throw exception between these two (or otherwise forget to RenderEndTag), it results in malformed html/xml. So, I wrote extension method over RenderBeginTag, which returns IDisposable class simply calling RenderEndTag, and I now clearly see, (via indentation) where is tag opened/closed. Another examples is measuring of how long did some block take to run. But, YMMV.
@Yossarian - this technique is called RAII (Resource Acquisition Is Initialization). @Kobi - RAII is a cornerstone of reliable software; it is not heavily used currently in .NET (for a variety of reasons), but is entering mainstream (witness, e.g., the Rx library).
@Yossarian - note the "Nothing in here"; it is that that makes it unusual
|
2

By not assigning the instance to a variable, it will be eligible for GC as soon as it goes out of scope.

Or, is there something else you are trying to accomplish with this?

1 Comment

Nothing else. As Marc underlined I was running my actions in the class constructor which seems to be bad (or at least unusual) practice. I don't need the class further then that.
0

Recommended reading:

http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.