Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Console.Out.WriteLine exist?

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?

like image 272
Kredns Avatar asked Jul 19 '09 02:07

Kredns


People also ask

Where is the output of console WriteLine?

In Visual Studio uppermost menu choose Debug > Windows > Output. It shows all Console. WriteLine("Debug MyVariable: " + MyVariable) when you get to them.

What is console WriteLine () good for?

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.

What is console in console WriteLine in C#?

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.

Where does console WriteLine write to in C#?

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.


2 Answers

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".

like image 38
Abhilash NK Avatar answered Sep 21 '22 07:09

Abhilash NK


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.

like image 87
Sam Harwell Avatar answered Sep 21 '22 07:09

Sam Harwell