How to perform input and output operations in C#?
In C#, you can use the Console class for input and output operations. Here are some commonly used input and output methods:
- Receive user input:
Console.WriteLine("请输入一个字符串:");
string input = Console.ReadLine();
Console.WriteLine("您输入的字符串是:" + input);
- Print information to the console.
Console.WriteLine("Hello, World!");
- Format output:
int num = 10;
Console.WriteLine("数字{0}的平方是{1}", num, num*num);
- Prompt the user to enter an integer.
Console.WriteLine("请输入一个整数:");
int num = int.Parse(Console.ReadLine());
Console.WriteLine("您输入的整数是:" + num);
It is important to note that when taking user input, data type conversion should be performed to ensure that the input data type matches the expected type, in order to avoid exceptions.