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:

  1. Receive user input:
Console.WriteLine("请输入一个字符串:");
string input = Console.ReadLine();
Console.WriteLine("您输入的字符串是:" + input);
  1. Print information to the console.
Console.WriteLine("Hello, World!");
  1. Format output:
int num = 10;
Console.WriteLine("数字{0}的平方是{1}", num, num*num);
  1. 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.

广告
Closing in 10 seconds
bannerAds