logo
Учебник_ПОА

Console Input and Output

C# console programs generally use the input/output services provided by .NET Framework Console class. The statement Console.WriteLine("Hello, World!"); uses the WriteLine method. It displays its string parameter on the command-line window followed by a new line. Other Console methods are used for different input and output operations. The Console class is a member of the System namespace. If the using System; statement was not included at the beginning of the program, you would have to specify the System classes like this:

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

The WriteLine method is very useful, and you will use it a lot if you are writing console applications.

WriteLine can display strings:

Console.WriteLine("Hello World!");

WriteLine can also display numbers:

int x = 42;

Console.WriteLine(x);

If you need to display several items, use {0} to represent the first item, {1} the second item, and so on, like this:

int year = 1066;

string battle = "Battle of Hastings";

Console.WriteLine("The {0} took place in {1}.", battle, year);

The output will look like this:

The Battle of Hastings took place in 1066.