【发布时间】:2013-10-23 16:56:36
【问题描述】:
给定代码:
// person.cs
using System;
// #if false
class Person
{
private string myName = "N/A";
private int myAge = 0;
// Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
// Declare an Age property of type int:
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}
public override string ToString()
{
return "Name = " + Name + ", Age = " + Age;
}
public static void Main()
{
Console.WriteLine("Simple Properties");
// Create a new Person object:
Person person = new Person();
// Print out the name and the age associated with the person:
Console.WriteLine("Person details - {0}", person);
// Set some values on the person object:
person.Name = "Joe";
person.Age = 99;
Console.WriteLine("Person details - {0}", person);
// Increment the Age property:
person.Age += 1;
Console.WriteLine("Person details - {0}", person);
}
}
// #endif
代码的输出是:
Simple Properties
Person details - Name = N/A, Age = 0
Person details - Name = Joe, Age = 99
Person details - Name = Joe, Age = 100
Console.WriteLine("Person details - {0}", person); 中的{0} 代表什么?
怎么换成了Name.....?
当我输入 {1} 而不是 {0} 时,我得到了一个例外......
【问题讨论】:
-
查找该方法的文档。它解释了它,非常详细。
-
补充一下Servy所说的,可以通过谷歌搜索“Console.WriteLine”找到文档。 msdn.microsoft.com/en-us/library/828t9b9h.aspx
标签: c# console console.writeline