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.
Консольный ввод и вывод
Программы консоли на C# обычно используют службы ввода/вывода, предоставляемые классом .NET Framework Console. Оператор Console.WriteLine("Hello, World!"); использует метод WriteLine. Он выводит свой строковый параметр в окно командной строки с новой строки. Другие методы Console используются для различных операций ввода и вывода. Класс Console является членом пространства имен System. Если в начало программы поместить директиву[34] using System;, то нужно будет указать классы System, как в следующем примере.
System.Console.WriteLine("Hello World!");Метод WriteLine очень полезен и часто используется при создании консольных приложений.
Метод WriteLine может отображать строки.
Console.WriteLine("Hello World!");Метод WriteLine может также отображать числа.
int x = 42;Console.WriteLine(x);Если необходимо отобразить несколько элементов, используйте {0} для представления первого элемента, {1} — для второго элемента и так далее, следующим образом.
int year = 1066;string battle = "Battle of Hastings";Console.WriteLine("The {0} took place in {1}.", battle, year);Результат будет выглядеть следующим образом. [35]
The Battle of Hastings took place in 1066.
Дата добавления: 2022-05-27; просмотров: 132;