return ten person nanes from spain in csharp

Assuming you have a list of people with information such as name and nationality, stored in an array or a list. You can return ten person names from Spain using the following code:

main.cs
// Sample list of people with properties including name and nationality
List<Person> peopleList = new List<Person>();

// Use a foreach loop to iterate through the list of people and retrieve 10 names that have nationality of Spain
int count = 0;
foreach (var person in peopleList)
{
    if (person.Nationality.ToLower() == "spain")
    {
        Console.WriteLine(person.Name);
        count++;
    }
    if (count == 10)
    {
        break;
    }
}
452 chars
18 lines

In this example, we use a foreach loop to iterate through the list of people and check if their nationality is Spain. If the person's nationality is Spain, we print their name to the console using Console.WriteLine(). We use a count variable to ensure that we only print 10 person names that have the nationality of Spain. Once the count reaches 10, we break out of the loop to stop the iteration.

gistlibby LogSnag