CSharp - Essential Types Console class


static Console class handles standard input/output for console-based applications.

In a command-line Console application, the input comes from the keyboard via Read, ReadKey, and ReadLine.

The output goes to the text window via Write and WriteLine.

You can control the window's position and dimensions with the properties WindowLeft, WindowTop, WindowHeight, and WindowWidth.

You can change the BackgroundColor and ForegroundColor properties and manipulate the cursor with the CursorLeft, CursorTop, and CursorSize properties:

Console.WindowWidth = Console.LargestWindowWidth;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write ("asdf");
Console.CursorLeft -= 3;
Console.Write ("dasdfasdf");

Write and WriteLine methods are overloaded to accept a composite format string.

Console.Out property returns a TextWriter.

Passing Console.Out to a method that expects a TextWriter can write to the Console for diagnostic purposes.

You can redirect the Console's input and output streams via the SetIn and SetOut methods:

// First save existing output writer:
System.IO.TextWriter oldOut = Console.Out;

// Redirect the console's output to a file:
using (System.IO.TextWriter w = System.IO.File.CreateText("e:\\output.txt"))
   Console.SetOut (w);
   Console.WriteLine ("Hello world");
// Restore standard console output
Console.SetOut (oldOut);

// Open the output.txt file in Notepad:
System.Diagnostics.Process.Start ("e:\\output.txt");


using System;
class MainClass/* ww  w.jav  a2s.com*/
   public static void Main(string[] args)
       Console.WriteLine("from console window");