Why does Console.Out.WriteLine exist?
Asked Answered
I

2

38

Actually the question should be why does Console.WriteLine exist just to be a wrapper for Console.Out.WriteLine

I found this little method using intellisense, then opened .NET reflector and 'decompiled' the code for the Console.WriteLine method and found this:

public static void WriteLine(string value)
{
    Out.WriteLine(value);
}

So why is WriteLine implemented this way? Is it totally just a shortcut or is there another reason?

Interfile answered 19/7, 2009 at 2:56 Comment(0)
G
66

Console.WriteLine is a static method. Console.Out is a static object that can get passed as a parameter to any method that takes a TextWriter, and that method could call the non-static member method WriteLine.

An example where this would be useful is some sort of customizable logging routines, where you might want to send the output to stdout (Console.Out), stderr (Console.Error) or nowhere (System.IO.TextWriter.Null), or anything else based on some runtime condition.

Goggleeyed answered 19/7, 2009 at 2:57 Comment(0)
P
25

Brad Abrams (The founding member of both CLR and .NET framework at Microsoft) says the following.

Console.WriteLine() is simply a shortcut for Console.Out.WriteLine. Console was overloaded by WriteLine propery to make that much easier to write.

Source: Book "The C# Programming Language by Anders Hejlsberg".

Palaver answered 22/11, 2016 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.