ObjectDisposedException when outputting to console
- by Sarah Vessels
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?