See Also: Console Members
The Console class provides basic input and output support for applications that read from and write characters to the console. If the console does not exist, as in a GUI application, writing to the console produces no result, and no exception is raised.
The standard input, output, and error streams are represented by properties, and are automatically associated with the console when the application starts. Applications can redirect these properties to other streams; for example, streams associated with files instead of the console.
The write methods support writing data with or without automatically appending carriage return and linefeed characters. This enables the writing of strings, formatted strings, arrays of characters, instances of primitive types, and arbitrary objects without first having to convert them to strings.
This class uses synchronized System.IO.TextReader and System.IO.TextWriter instances. Multiple threads can concurrently read from and/or write to an instance of this type.
The following example demonstrates the use of basic Console input and output functions. The program waits for the user to enter a name.
C# Example
using System;
public class ConsoleTest {
public static void Main() {
Console.Write("Hello ");
Console.WriteLine("World!");
Console.Write("What is your name: ");
string name = Console.ReadLine();
Console.Write("Hello, ");
Console.Write(name);
Console.WriteLine("!");
}
}
The output for a user who entered the name "Fred" is
Hello World!