No new types. No objects explicitly created. Works in a real Console application.
public class BaseHome
{
static System.IO.TextWriter Console
{
get
{
System.Console.Write(" C\rB");
return System.Console.Out;
}
}
public static void Main()
{
Console.WriteLine("A");
// System.Console.ReadLine();
}
}
Result is BAC
- on the same line no less!
(This can be adapted to multiple line output, per the post edit, with the use of CurstorLeft/Top or direct escape sequences.)
Explanation:
The static property (Console
) is resolved instead of the type in Console.WriteLine("A")
as the property shadows the type here; this is why System.Console is used to refer to the Console class itself.
The Console get-property causes a side-effect of writing to the console - it writes "__C" then uses CR (Carriage Return) to "return to the line start" and writes "B" so the line is "B_C", leaving the cursor after the "B".
The property then returns the TextWriter associated with the console which has WriteLine. The TextWriter's WriteLine, not Console's static WriteLine, is then invoked and the character "A" is written (after "B") so the result is "BAC".
This utilizes context-specific behavior because it is the console which understands how to move the cursor (e.g. with "\r" or other cursor positioning).