views:

77

answers:

5

My code looks like the below. Obviously i cant write 'ok' because the object has been disposed. I cant do return sw.Clone() bc clone doesnt exist. If i dont use a using then at any point between = new and return (like iterating and writing to the object as my example doesnt do) can have an exception and thus not disposing the object.

Am i to define sw outside of a try block and check if its null then dispose in a catch block? That seems like a bit of excessive work. Is there a better way? is that the only way?

    static void func1()
    {
        using (var sw = func2())
        {
            sw.WriteLine("Ok");
        }
    }
    static StringWriter func2()
    {
        using (var sw = new StringWriter())
        {
            return sw;
        }
    }
A: 

Create a class with the func1 and func2 methods, make that IDisposable, initialize a StringWriter in the constructor, and Dispose of it in your class's Dispose method.

This assumes you're writing to the StringWriter in more than one method.

Matthew Flaschen
A: 

Get rid of the second using block. Just do

StringWriter sw = new StringWriter();
return sw;

The StringWriter gets disposed if you're defining it in a using block.

halfdan
That doesn't work if an exception thrown between `new StringWriter` and `return sw` (for example, if you do stuff with the StringWriter before you return it)
Dean Harding
A: 

FYI: The implementation of StringWriter's close calls the Dispose method passing a true value. Why not just return the second sw to the calling method and call close which will the equivalent.

RandomNoob
A: 

I'm going to assume this is a general question about returning objects which implement IDisposable, and not a question about func1 and func2.

Use a using block around anything implementing IDisposable if you

  1. Create it and
  2. No longer need it when your method returns

In your case, you are creating it, but it needs to still exist after your method returns. In this case, don't use a using block in the method that creates the object. It then becomes the responsibility of the caller to dispose of the object, which I see it already does.

John Saunders
The problem i am asking is the exception in between that Code Analysis warns me about that can leave my object not being disposed. Assuming i am doing best practices i should handle this but think a try/catch is overkill. Maybe i should return a string
acidzombie24
Please show the exact code that produces a Code Analysis warning, and please show the warning. BTW, if you can return a string, return a string.
John Saunders
+1  A: 

You may want to reconsider returning a StringWriter. While you may have a few places where you need that extra functionality, it feels to me like plumbing sticking up in the middle of the living room. It shouldn't be part of a public API, and is kinda clunky even in a private one.

With that said, if you need one, don't Close or Dispose it before returning it, and don't use a using block. Use a try/catch block (ie: Dispose in the catch, not the finally clause).

cHao
I may choose this because your the only one that said i should consider not returning stringwriter/using that functionality.
acidzombie24