小问题
我现在还在学习C#中 `看到红皮书的第11章 `
做到后面的练习中有个地方想问
public class Person
{
private string name;
private int age;
public string Name
{
get
{
return name;
}
set
{
name =value ;
}
}
public int Age
{
get
{
return age ;
}
set
{
age = value;
}
}
public Person(string newName,int newAge)
{
Name = newName;
Age = newAge;
}
}
public class people :DictionaryBase
{
public void Add(string personID,Person newperson)
{
Dictionary.Add(personID, newperson);
}
public void Remove(string personID)
{
Dictionary.Remove(personID);
}
public people()
{
}
public Person this[string personID]
{
get
{
return (Person)Dictionary[personID];
}
set
{
Dictionary[personID] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
people peopleTOP = new people();
peopleTOP.Add("john", new Person("john",19));
peopleTOP.Add("lannet",new Person("lannet",20));
foreach (DictionaryEntry mypeople in peopleTOP)
{
//为什么这里用下面的这种方法就可以而我用
//Console.WriteLine(((Person )mypeople .Value ).Name,((Person )mypeople .Value ).Age);
// 不会产生问题却不能显示出age的值?
Console.WriteLine(((Person )mypeople .Value ).Name);
Console .WriteLine (((Person )mypeople .Value ).Age);
}
}
}