Writing Console Output

Console.Out and Console.Error are objects of type TextWriter. Console output is most easily accomplished with Write( ) and WriteLine( ), with which you are already familiar. Versions of these methods exist that output each of the built-in types. Console defines its own versions of Write( ) and WriteLine( ) so they can be called directly on Console. However, you can invoke these (and other) methods on the TextWriter that underlies Console.Out and Console.Error, if you choose.
 
Here is a program that demonstrates writing to Console.Out and Console.Error. By default, both write to the console.

Example

using System;

namespace ConsoleApplication1
{
    class ErrOut
    {
        static void Main()
        {
            int a = 10, b = 0;
            int result;
            Console.Out.WriteLine("This will generate an exception.");
            try
            {
                result = a / b; // generate an exception
            }
            catch (DivideByZeroException exc)
            {
                Console.Error.WriteLine(exc.Message);
            }
        }
    }
}

Sometimes newcomers to programming are confused about when to use Console.Error. Since both Console.Out and Console.Error default to writing their output to the console, why are there two different streams? The answer lies in the fact that the standard streams can be redirected to other devices. For example, Console.Error can be redirected to write to a disk file, rather than the screen. Thus, it is possible to direct error output to a log file, for example, without affecting console output. Conversely, if console output is redirected and error output is not, then error messages will appear on the console, where they can be seen.