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?
In Visual Studio uppermost menu choose Debug > Windows > Output. It shows all Console. WriteLine("Debug MyVariable: " + MyVariable) when you get to them.
Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line.
Console. WriteLine("This is C#"); In this code line, we print the "This is C#" string to the console. To print a message to the console, we use the WriteLine method of the Console class. The class represents the standard input, output, and error streams for console applications.
First, Console. WriteLine() writes to the standard output stream which can be read by attaching a console window or through other means of intercepting the stream. Then, to send your output to a file you could setup a TraceListener which could be a file or something.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With