Console.WriteLine()是C#中用于打印輸出的方法。它可以接受一個或多個參數,并將它們輸出到控制臺窗口。
下面是一些使用Console.WriteLine()方法的例子:
string name = "John";
Console.WriteLine("My name is " + name);
int age = 25;
Console.WriteLine("I am " + age + " years old");
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}");
int number = 42;
Console.WriteLine("The answer to the ultimate question of life, the universe, and everything is {0:D}", number);
上述例子中的Console.WriteLine()方法會將參數中的內容輸出到控制臺窗口,并自動換行。