Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 524293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:33:09+00:00 2026-05-13T08:33:09+00:00

I have 2 classes and in the Persons class I want to add the

  • 0

I have 2 classes and in the Persons class I want to add the ability of looping through the name properties of the Person collection using a foreach loop, like this:

foreach (string name in Persons.Names)
{
// do something
}

How would I do this?

These are my classes:

 class Person
    {
        public string Name
        {
            get;
            set;
        }

        public string Surname
        {
            get;
            set;
        }
    }

    class Persons : IEnumerable<Person>
    {
        List<Person> persons = new List<Person>();
        public IEnumerator<Person> GetEnumerator()
        {
            foreach (Person p in persons)
            {
                yield return p;
            }
        }

        System.Collections.IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public IEnumerator<string> Names
        {
            // implement looping through Name property of Person class
        }

        public Persons()
        {

        }

        public void Add( Person p )
        {
            persons.Add(p);
        }

        public string this[int index]
        {
            get { return this[index]; }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:33:09+00:00Added an answer on May 13, 2026 at 8:33 am

    The first loop can be implemented something like this:

    foreach (Person p in Persons)
    {
    // do something with p.Name
    }
    

    You can implement the property something like:

    public IEnumerator<string> Names
    {
        get
        {
            foreach (Person p in persons)
            {
                yield return p.Name;
            }
        }
    }
    

    or using Linq:

    public IEnumerator<string> Names
    {
        get
        {
            return persons.Select(p => p.Name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes class Person { private String name; void getName(){...}} class
I have the following classes: class Person { public string Name { get; set;
I have these classes. class Author{ Person person } class Person{ String lastName String
I have these two classes public class Person { } public class Company {
I have there domain classes: Person. (Person.ID, Name,Address) Designation.(Designation.ID, Title, Band) SalarySlip (Person.ID, Designation.ID,
I have classes structured like this: Public MustInherit Class A ' several properties End
I have the following (simplified) classes: public class Person { public string Id {get;
I have a collection of objects that derive from a Person class and I
I have the following classes public class Person { public virtual int Id {
I have the following classes public class Person { public virtual int Id {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.