刚学C#,请高手c#程序解释(不要笑我哟)

来源:百度知道 编辑:UC知道 时间:2024/07/04 20:18:11
using System;
public class student
{
private string Sid;
static int count;
public student(string id)
{
Sid=id;
count++;
}
public void display()
{
Console.WriteLine("Sid:",Sid); //Sid:{0}为什么不写成Sid:
Console.WriteLine("count:",count);//同上
}
}
class ClassTest
{
static void Main(string[] args)
{
student s1=new student("10001");
s1.display();
student s2=new student("10002");
s2.display();
}
}
问题是1:为什么Sid:{0}为什么不写成Sid:为什么count:{0}为什么不写成count:
2.没有{0}时运行如下:
sid:
count:
sid:
count:

O(∩_∩)O哈哈~
我刚学C#那会,也是对这个有点疑问。不过看多了就自然明白了。

Console.WriteLine("Sid:{0}",Sid);
把Sid:写成Sid:{0},意思是,{0}就是指,Console.WriteLine("Sid:{0}",Sid)中的逗号后面的Sid.
Console.WriteLine("Sid:{0}",Sid),这样写就是格式化字符串。
只要记住一点,{0},就是你要替换的参数,它在这里只是占个位置。

所以呢,
Console.WriteLine("Sid:",Sid); //Sid:{0}为什么不写成Sid:
Console.WriteLine("count:",count);//同上
以上两句,sid:和count:后面你都没有加{0},当然在界面没有显示了。

扩展一下:
Console.WriteLine("a:{0},b:{1},c:{2}{}",A,B,C);
{0},{1},{2},在{}中数字是递增的。意思就是
{0}----->A
{1}------>B
{2}------>C
是一一对应的。它有三个参数,所以就是0,1,2了。

如果还有什么疑问
QQ:286598435
Email:software.net.wu@gmail.com
随时可以联系的。
呵呵~~~~祝学习进步。

string name = "张三";
int age = 20;
Console.WriteLine("你的名字{0},年龄是{1}",name,age);
输出则是:你的名字张三,年龄是20;

如果Console.WriteLine("你的名字name,年龄是age");
则输出你的名字nam