在 C# 中打印单个和多个变量


若要在 C# 中显示单个变量值,只需使用 Console.WriteLine()

让我们看一个示例。此处,我们在单行中显示了单个变量“a”的值 −

示例

using System;
using System.Linq;

class Program {
   static void Main() {
      int a = 10;
      Console.WriteLine("Value: "+a);
   }
}

若要在 C# 中显示多个变量值,您需要在 Console.WriteLine() 中使用逗号运算符。

让我们看一个示例。此处,我们在单行中显示了多个变量“a”和“b”的值 −

示例

using System;
using System.Linq;

class Program {
   static void Main() {
      int a = 10;
      int b = 15;
      Console.WriteLine("Values: {0} {1} ",a,b);
   }
}

上面,{0} 被变量 a 的值替换,而 {1} 被变量 b 的值替换。

更新于: 2020 年 6 月 21 日

9000 多次阅读

职业起航

完成课程获得认证

开始
广告
© . All rights reserved.