Export (0) Print
Expand All
16 out of 38 rated this helpful - Rate this topic

Console Class

Represents the standard input, output, and error streams for console applications. This class cannot be inherited.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Public NotInheritable Class Console
'Usage
The members of a static class are accessed directly without an instance of the class.
public final class Console
public final class Console

The console is an operating system window where users interact with the operating system or a text-based console application by entering text input through the computer keyboard, and reading text output from the computer terminal. For example, in Windows the console is called the command prompt window and accepts MS-DOS commands. The Console class provides basic support for applications that read characters from, and write characters to, the console.

Console I/O Streams

When a console application starts, the operating system automatically associates three I/O streams with the console. Your application can read user input from the standard input stream; write normal data to the standard output stream; and write error data to the standard error output stream. These streams are presented to your application as the values of the In, Out, and Error properties.

By default, the value of the In property is a System.IO.TextReader object, and the values of the Out and Error properties are System.IO.TextWriter objects. However, you can set these properties to streams that do not represent the console; for example, you can set these properties to streams that represent files. I/O operations using these streams are synchronized, which means multiple threads can read from, or write to, the streams.

Console class members that work normally when the underlying stream is directed to a console might throw an exception if the stream is redirected, for example, to a file. Consequently, program your application to catch System.IO.IOException if you redirect a standard stream.

It is sometimes useful to explicitly invoke the members of the stream objects represented by the In, Out, and Error properties. For example, by default, the WriteLine method writes data to the standard output stream followed by the default line termination string; that is, data is followed by a carriage return and line feed ("\r\n"). However, the Console class does not provide a method to write data to the standard error output stream, or a property to change the line termination string for data written to that stream.

You can solve this problem by setting the TextWriter.NewLine property of the Out or Error property to another line termination string. For example, the C# statement, Console.Error.NewLine = "\r\n\r\n";, sets the line termination string for the standard error output stream to two carriage return and line feed sequences. Then you can explicitly call the WriteLine method of the error output stream object, as in the C# statement, Console.Error.WriteLine();.

Screen Buffer and Console Window

Two closely related features of the console are the screen buffer and the console window. Text is actually read from or written to streams owned by the console, but appear to be read from or written to an area owned by the console called the screen buffer. The screen buffer is an attribute of the console, and is organized as a rectangular grid of rows and columns where each grid intersection, or character cell, can contain a character. Each character has its own foreground color and each character cell has its own background color.

The screen buffer is viewed through a rectangular region called the console window. The console window is another attribute of the console; it is not the console itself, which is an operating system window. The console window is also arranged in rows and columns, is less than or equal to the size of the screen buffer, and can be moved to view different areas of the underlying screen buffer. If the screen buffer is larger than the console window, the console automatically displays scroll bars so the console window can be repositioned over the screen buffer area.

A cursor indicates the screen buffer position where text is currently read or written. The cursor can be hidden or made visible, and its height can be changed. If the cursor is visible, the console window position is moved automatically so the cursor is always in view.

The origin for character cell coordinates in the screen buffer is the upper left corner, and the position of the cursor and the console window are measured relative to that origin. Use zero-based indexes to specify positions; that is, specify the topmost row as row 0, and the leftmost column as column 0. The maximum value for the row and column indexes is Int16.MaxValue.

Functionality

The Console class contains methods that read individual characters or entire lines from the console; and several write methods that automatically convert an instance of a value type, an array of characters, or sets of objects to a formatted or unformatted string, and then writes that string, optionally followed by a line termination string, to the console. The Console class also contains methods and properties to get or set the size of the screen buffer, console window, and cursor; to change the position of the console window and cursor; to move or clear data in the screen buffer; to change foreground and background colors; to change the text displayed in the console title bar; and to play the sound of a beep.

This code sample demonstrates how to read from and write to the standard input and output streams. Note that these streams can be redirected using the SetIn and SetOut methods.

Public Shared Sub Main()
    Console.Write("Hola ")
    Console.WriteLine("Mundo!")
    Console.WriteLine("What is your name: ")
    Dim name As String = Console.ReadLine()
    Console.Write("Buenos Dias, ")
    Console.Write(name)
    Console.WriteLine("!")
End Sub 'Main

public static void main(String[] args)
{
    Console.Write("Hola ");
    Console.WriteLine("Mundo!");
    Console.WriteLine("What is your name: ");
    String name = Console.ReadLine();
    Console.Write("Buenos Dias, ");
    Console.Write(name);
    Console.WriteLine("!");
} //main

Console.Write("Hola ");
Console.WriteLine("Mundo!");
Console.WriteLine("What is your name: ");
var name : String = Console.ReadLine();
Console.Write("Buenos Dias, ");
Console.Write(name);
Console.WriteLine("!");

This type is safe for multithreaded operations.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.