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

  • Home
  • SEARCH
  • 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 1042893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:34:50+00:00 2026-05-16T15:34:50+00:00

I have a data class Student, and I have an aggregate class Students. Student

  • 0

I have a data class Student, and I have an aggregate class Students.
Student has two properties of type string : Name and City.

what I want to do is have the option to choose what property to iterate using the foreach mechanism.

The code I wrote works and it’s also readable and nice-looking.
The main issue is performance : the line in which I use the yield keyword is probably not very efficient , but the question is how much ? is it dramatic performance hit ?

Is there a better way to achieve this functionality?
(added: i don’t want to allow someone to modify the returning Student objects, so all the Linq solutions proposed aren’t good here. To make it clearer , I want:
property iteration + foreach mechanism integration + Student class and the list of students are readonly.
how can i achieve that ?)

static void Main(string[] args)
    {           
        Students students = new Students();

        students.AddStudent(new Student { Age = 20, Name = "Stud1" , City="City1" });
        students.AddStudent(new Student { Age = 46, Name = "Stud2" , City="City2"});
        students.AddStudent(new Student { Age = 32, Name = "Stud3" , City="City3" });
        students.AddStudent(new Student { Age = 34, Name = "Stud4" , City="city4" });

        students.PropertyToIterate = eStudentProperty.City;
        foreach (string studentCity in students)
        {
            Console.WriteLine(studentcity);
        }

        students.PropertyToIterate = eStudentProperty.Name;
        foreach (string studentName in students)
        {
            Console.WriteLine(studentName);
        }

    }

public class Students :IEnumerable<object>
{
    private List<Student> m_Students = new List<Student>();

    private eStudentProperty m_PropertyToIterate = eStudentProperty.Name;

    public eStudentProperty PropertyToIterate
    {
        get { return m_PropertyToIterate; }
        set { m_PropertyToIterate = value; }
    }

    public void AddStudent(Student i_Student)
    {
        m_Students.Add(i_Student);
    }

    public IEnumerator<object> GetEnumerator()
    {            
        for (int i = 0; i < m_Students.Count; ++i)
        {
            yield return (object)m_Students[i].GetType().GetProperty(PropertyToIterate.ToString()).GetValue(m_Students[i], null);
        }            
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

public enum eStudentProperty
{
    Name,
    Age,
    City
} 

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

    public string City { get; set; }

    public int Age { get; set; }
}
  • 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-16T15:34:51+00:00Added an answer on May 16, 2026 at 3:34 pm

    In response to your edit, how about something like this…

    Students students = new Students();
    students.AddStudent(new Student { Age = 20, Name = "Stud1", City = "City1" });
    students.AddStudent(new Student { Age = 46, Name = "Stud2", City = "City2" });
    students.AddStudent(new Student { Age = 32, Name = "Stud3", City = "City3" });
    students.AddStudent(new Student { Age = 34, Name = "Stud4", City = "city4" });
    
    foreach (int studentAge in students.EnumerateBy(StudentProperty.Age))
    {
        Console.WriteLine(studentAge);
    }
    
    foreach (string studentName in students.EnumerateBy(StudentProperty.Name))
    {
        Console.WriteLine(studentName);
    }
    
    foreach (string studentCity in students.EnumerateBy(StudentProperty.City))
    {
        Console.WriteLine(studentCity);
    }
    
    // ...
    
    public class Students
    {
        private List<Student> _students = new List<Student>();
    
        public void AddStudent(Student student)
        {
            _students.Add(student);
        }
    
        public IEnumerable<T> EnumerateBy<T>(StudentProperty<T> property)
        {
            return _students.Select(property.Selector);
        }
    }
    
    public static class StudentProperty
    {
        public static readonly StudentProperty<int> Age =
            new StudentProperty<int>(s => s.Age);
    
        public static readonly StudentProperty<string> Name =
            new StudentProperty<string>(s => s.Name);
    
        public static readonly StudentProperty<string> City =
            new StudentProperty<string>(s => s.City);
    }
    
    public sealed class StudentProperty<T>
    {
        internal Func<Student, T> Selector { get; private set; }
    
        internal StudentProperty(Func<Student, T> selector)
        {
            Selector = selector;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.