温馨提示×

C#中的Console.WriteLine()怎么用

c#
小亿
103
2024-01-22 17:56:07
栏目: 编程语言

Console.WriteLine()是C#中用于打印输出的方法。它可以接受一个或多个参数,并将它们输出到控制台窗口。

下面是一些使用Console.WriteLine()方法的例子:

  1. 输出字符串类型的内容:
string name = "John";
Console.WriteLine("My name is " + name);
  1. 输出数字类型的内容:
int age = 25;
Console.WriteLine("I am " + age + " years old");
  1. 输出多个参数:
string firstName = "John";
string lastName = "Doe";
Console.WriteLine("My name is {0} {1}", firstName, lastName);

或者使用字符串插值(String Interpolation):

string firstName = "John";
string lastName = "Doe";
Console.WriteLine($"My name is {firstName} {lastName}");
  1. 输出格式化字符串:
int number = 42;
Console.WriteLine("The answer to the ultimate question of life, the universe, and everything is {0:D}", number);

上述例子中的Console.WriteLine()方法会将参数中的内容输出到控制台窗口,并自动换行。

0