Write是Console类的成员,将文本字符串发送到程序的控制台窗口。 在最简单的情况下,Write必须将文本字符串文字发送到窗口,并将字符串用双引号括起来。
console.write (' thisistrivitaltext.'; --this is trivital文本。
system.console.write (' this is text1. ';
system.console.write (' this is text2. ';
system.console.write (' this is text3. ';
请注意,Write不会在字符串后添加换行符,因此所有三个语句都将输出到同一行。 --
this is text1. this is text2. this is text 3。
WriteLine是控制台的另一个成员,提供与Write相同的功能,但在每个输出字符串的末尾添加换行符。
system.console.writeline (' this is text1. ';
system.console.writeline (' this is text2. ';
system.console.writeline (' this is text3. ';
--
This is text1。
This is text2。
This is text3。
格式字符串:
Write和WriteLine语句的常规格式可以包含多个参数。
如果有多个参数,则参数之间用逗号分隔。
第一个参数必须是称为格式字符串的字符串。 格式字符串可以包含替代标记。
语法如下:
Console.WriteLine (包含替代标记的格式字符串、替换值0、替换值1、替换值2、);
console.writeline (' twosampleintegersare {0} and {1}.',3,6 );
Two sample integers are 3 and 6。
字符串插值:通过在替代标记内直接插入变量名称来实现。 实际上,替代标记告诉编译器此变量名称将被视为变量,而不是字符串文字。 前提是字符串前面有$符号。
int var1=3;
int var2=6;
console.writeline ($ ' twosampleintegersare { var1} and { var2}.' );
Two sample integers are 3 and 6。
多个标签和值:
C#可以使用任意数量的替代标记和任意数量的值。
值可以按任意顺序使用。
您可以多次用格式字符串替换值。
console.writeline (' threeintegersare {1},{0} and {1}.',3,6 );
- threeintegersare 6,3 and6。
标记不能引用超过替换值列表长度的值。