Hi,
How does the WriteLine() Method know to print to the console?
Is is using deferred execution behind the scenes in that an expression tree of some sort is being used?
Regards,
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Apr 3, 2011, 5:41 AM EDT
In fact Console.WriteLine() is just a convenience method for the more long-winded Console.Out.WriteLine() where the Out property is a reference to the output stream.
It was introduced in .NET 1.0 and certainly doesn't use deferred execution or expression trees which were much later additions.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 3, 2011, 10:41 AM EDT
The C++ language defines standard input and standard output devices, which are typically a console window. The concept of a console is the result of the time period when C++ was designed, in which dumb terminals were used with computers. I don't know the C# language has a similar definition but it probably does. I assume that the C# language defines the Console class to be an object that could be the equivalent of a dumb terminal, although I am sure they don't use the term dumb terminal. A Console is likely defined to be a text-only window.
VulpesRank #55 · 29.2K reputationPosted Apr 3, 2011, 11:49 AM EDT
All the Console API functions (see http://msdn.microsoft.com/en-us/library/ms682087(VS.85).aspx) can be P/Invoked from C# and work fine if you need any extra functionality, such as mouse support, which is not wrapped by the System.Console class.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 3, 2011, 12:02 PM EDT
I had assumed that the .Net Console reads and writes could be redirected; is that incorrect? I thought that the Windows API console functions could not be redirected.
VulpesRank #55 · 29.2K reputationPosted Apr 3, 2011, 2:03 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Apr 3, 2011, 8:43 PM EDT
"ReadConsole and WriteConsole fail if used with a standard handle that has been redirected and is no longer a console handle."
....
"The functions do not return keyboard events involving the function keys or arrow keys. Input events generated by mouse, window, focus, or menu input are discarded."
....
if "line input mode is enabled" .... "do not return to the calling application until the ENTER key is pressed"
Just think about it. Streams only move forward; the console API is used to randomly move around the screen. The two are fundamentally incompatible.
VulpesRank #55 · 29.2K reputationPosted Apr 4, 2011, 2:31 AM EDT
However, we can still use the ReadFile() and WriteFile() functions whether the standard input and output has been redirected or not. If passed a console handle, these functions (in effect) treat the console buffer as though it were a file.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 4, 2011, 8:15 AM EDT