Переопределить ToString()
, что-то вроде этого:
class Person
{
.....
.....
public override string ToString() {
return name;
}
}
Таким образом, вы можете сделать:
person kalle = new person();
kalle.name = "Hello";
string value = falle.ToString();
Итак, представление строки kalle
здесь становится «Привет» (содержание name
свойство)
Если это не то, о чем вы просите, уточните.
РЕДАКТИРОВАТЬ
Пример чтения более одного
string s = null;
while((s = Console.ReadLine())!="x") {
Console.WriteLine("how many people would you add?");
int howMany = Console.ReadLine();
for(int i=0;i<howMany ;i++)
{
//create Person objects and add them to the collection
//ask user to insert the name for current Person
Console.WriteLine(string.Format("Please insert the name for the Person {0}", i));
//read the name
string name = Console.ReadLine();
//construct Person object, based on (i) and (name)
}
}
Надеюсь, это то, что вы просите.