ObjectDisposedException when outputting to console

Posted by Sarah Vessels on Stack Overflow See other posts from Stack Overflow or by Sarah Vessels
Published on 2010-05-25T19:08:27Z Indexed on 2010/05/25 19:11 UTC
Read the original article Hit count: 340

Filed under:
|
|
|
|

If I have the following code, I have no runtime or compilation problems:

if (ConsoleAppBase.NORMAL_EXIT_CODE == code)
{
    StdOut.WriteLine(msg);
}
else
{
    StdErr.WriteLine(msg);
}

However, in trying to make this more concise, I switched to the following code:

(ConsoleAppBase.NORMAL_EXIT_CODE == code
    ? StdOut
    : StdErr
).WriteLine(msg);

When I have this code, I get the following exception at runtime:

System.ObjectDisposedException: Cannot write to a closed TextWriter

Can you explain why this happens? Can I avoid it and have more concise code like I wanted?

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception