如何在控制台应用程序 C# 中打印值

How to print a value in console application C#

这是 运行 在我的控制台上的代码。

public static void Main(string[] args)
{
    string X = GeneratePassword();
    Console.WriteLine("The password = ", X);
    Console.ReadLine();
    Console.ReadKey();
}

我的 GeneratePassword 函数肯定会返回值,但不知何故我无法打印它。我可能遗漏了一件小事。请帮忙

提前致谢。

改变

 Console.WriteLine("The password = "+ X);

Console.WriteLine("The password = {0}", X);

替换

Console.WriteLine("The password = ", X);

Console.WriteLine("The password = {0}", X);

您缺少变量的占位符。 如果你有更多的变量,那就是

 Console.WriteLine("The password = {0} and other value {1}", X, Y);

编辑: 您也可以使用

Console.WriteLine("The password = "+ X);
//or note the $ sign before string. It will render variables inside {} 
Console.WriteLine($"The password = {X}");

您可以选择任意一种方式:

Console.WriteLine("The password = " + x);

或者

Console.WriteLine(string.Format("The password = {0}", x));