Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Console Class

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

For a list of all members of this type, see Console Members.

System.Object
   System.Console

[Visual Basic]
NotInheritable Public Class Console
[C#]
public sealed class Console
[C++]
public __gc __sealed class Console
[JScript]
public class Console

Thread Safety

This type is safe for multithreaded operations.

Remarks

The Console class provides basic support for applications that read characters from, and write characters to, the console. If the console does not exist, as in a Windows-based application, writes to the console are not displayed and no exception is raised.

Data from the console is read from the standard input stream; normal data to the console is written to the standard output stream; and error data to the console is written to the standard error output stream. These streams are automatically associated with the console when your application starts, and are presented to you as the In, Out, and Error properties.

By default, the value of the In property is a System.IO.TextReader, while the values of the Out and Error properties are System.IO.TextWriter objects. However, you can associate these properties with streams that do not represent the console; for example, you can associate them with streams that represent files. You can also associate these properties with different streams at the same time.

This class uses synchronized TextReader and TextWriter instances. Multiple threads can concurrently read from or write to an instance of this type.

This class contains methods that read individual characters or entire lines from the console. This class 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, then writes that string to the console, optionally followed by a line termination string.

You can also explicitly invoke the method and property members of the stream objects represented by the In, Out, and Error properties. For example, the WriteLine method writes data to the standard output stream followed by a line termination string; that is, data followed by a carriage return and line feed ("\r\n"). However, there is no convenient method to write data to the standard error output stream. Instead, use an explicit invocation like the C# statement, Console.Error.WriteLine();.

To continue the previous example, the default value of the line termination string is a carriage return followed by a line feed. Change the line termination character sequence by setting the TextWriter.NewLine property of the Out or Error property to another string. For example, the C# statement, Console.Error.NewLine = "\r\n\r\n";, sets the line termination character sequence for the Console.Error.WriteLine method to two carriage return and line feed sequences.

Example

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.

[Visual Basic] 
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

[C#] 
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("!");
}

[C++] 
int main()
{
   Console::Write(L"Hola ");
   Console::WriteLine(L"Mundo!");
   Console::WriteLine(L"What is your name: ");
   String *name = Console::ReadLine();
   Console::Write(L"Buenos Dias, ");
   Console::Write(name);
   Console::WriteLine(L"!");
}

[JScript] 
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("!");

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Console Members | System Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft