C# Tutorial - Dissecting Our First Application - The Console Class


In order to interact with the user of the program we need to be able to output text to the screen. This is done through the use of the Console class that is located within the System namespace. The Console class provides basic support for applications that read characters from, and write characters to, the console window.

The Console class contains methods that can read individual characters or entire lines from the console. It also contains several write methods that automatically convert individual instances of value types, arrays of characters, or sets of objects to a formatted or unformatted string, before writing the string to the console.

The Console class enables input, output, and error streams for console applications. When the console application starts, the operating system automatically associates the three I/O streams with the console. If the console does not exist, as in a Windows-based application, writes to the console are ignored.

Data from the console is read from the standard input stream and is presented to the developer as In. The value of the In property is a System.IO.TextReader object, which represents a reader that can read a sequential series of characters.

Data to the console is written to the standard output stream and is presented to the developer as Out. The value of the Out property is a System.IO.TextWriter object, which represents a writer that can write a sequential series of characters.

Reading To and Writing From the Console Window

In our example, we write a single line of text to the console window using the method WriteLine. Then to stop the window from closing immediately, we use the method Read to read a key press from within the console window.

Console.WriteLine("Hello World!"); 
Console.Read();

If you remove the line Console.Read();, the window closes as soon as you run the application from within the IDE. However if you were to run the application from a command window, then this program will run just fine.

If you wish to run the program from the IDE without the line Console.Read();, but have the application run as if it was in the command window, then choose Start Without Debugging from the Debug menu, or use the shortcut key Ctrl+F5.

In .NET 2.0, the Console class was enhanced with additional functionality. The following table lists some of the new members available.

Members Description
BackgroundColor
ForegroundColor
These properties allow the background/foreground colours for the current output to be set. They may be assigned any member of the ConsoleColor enumeration.
BufferHeight
BufferWidth
These properties control the height/width of the console's buffer area.
Clear() This method clears the buffer and console display area.
Title This property sets the title of the current console.
WindowHeight
WindowWidth
WindowTop
WindowLeft
These properties control the dimensions of the console in relation to the established buffer.

Reading

To read a single character from the standard input stream, you use the Read method and store the result in a character or string.

char c = Console.Read();

To read a line of text from the standard input stream, you use the ReadLine method and store the result in a string.

string line = Console.ReadLine();

Writing

To write a character or string of characters to the console, you use the Write method.

Console.Write( "Hello World" );

If you wish the next text that you output to appear on a new line, then you can add a carriage return and line feed, (\r\n).

Console.Write( "Hello World\r\n" );

However, the Console class has a built in method to do this for you which automatically adds a line termination string. This is the WriteLine method.

Console.WriteLine( "Hello World" );

<< Previous Contents Next >>

© Publicjoe, 2008