"Doesn't work" doesn't give anyone enough information to help you, what is happening that you didn't expect to happen, or what isn't happening that you want to happen?
I'm going to assume your issue is partly in using ToString on the Person object. If you don't override ToString you'll just get the name of the type. So either be explicit in how you want the result to look;
string random = people[r.Next(people.Count)];
string result = random.Surname + ", " + random.Name;
or override ToString on the Person class to return the result you want. If the result has to be random Name or Surname you can do something like
public string ToString(Random random)
{
if (random.Next(2) == 0)
{
return this.Name;
}
else
{
return this.Surname;
}
}
public override string ToString()
{
return ToString(new Random());
}
I've done a standard ToString() and also one where you can pass in your own instance of Random. This is in case you want to run the code in a loop. Random uses the time as a seed for its random number generator so if you create new instances of Random in a loop you can end up with the same random number each time because enough time hasn't passed to generate a new seed\sequence.