0

I'm trying to loop through an ArrayList of objects of type (people) so i created two classes :

Person.cs

using System;

namespace GenericTypes
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public DateTime DateTime { get; set; }

    }
}

People.cs

using System;
using System.Collections;
using System.Collections.Generic;

namespace GenericTypes
{
    public class People
    {
        public ArrayList GetNonGenericPeople()
        {
            var people = new ArrayList()
            {
                new Person() {FirstName = "Djerah", LastName = "Ahmed Rafik", Age = 23, DateTime = DateTime.Today},
                new Person() {FirstName = "Djerah", LastName = "Amjed Amir", Age = 11, DateTime = DateTime.Today},
                new Person() {FirstName = "Gadda", LastName = "Anoir", Age = 25, DateTime = DateTime.Today}

            };
            return people;
        }

        public  List<Person> GetGenericPeople()
        {
            var people = new List<Person>()
            {
                new Person() {FirstName = "Djerah", LastName = "Ahmed Rafik", Age = 23, DateTime = DateTime.Today},
                new Person() {FirstName = "Djerah", LastName = "Amjed Amir", Age = 11, DateTime = DateTime.Today},
                new Person() {FirstName = "Gadda", LastName = "Anoir", Age = 25, DateTime = DateTime.Today}

            };

            return people;
        }
    }
}

What i couldn't figure out is how to loop through GetNonGenericPeople() so can i output every object with it's properties

program.cs

namespace GenericTypes
{
    class Program
    {
        static void Main(string[] args)
        {
           var persons = new People();
            var p = persons.GetNonGenericPeople();

            foreach (var s in p)
            {
                Console.WriteLine(s);
            }
        }
    }
}

1 Answer 1

2

Try OfType<T>() Linq which filters out T instances; another option is Cast<T>() which tries to cast each item (and throw exception if cast fails)

 foreach (var s in p.OfType<Person>()) {
   ...
 }
Sign up to request clarification or add additional context in comments.

3 Comments

@D.AhmedRafik: you're welcome! However remember that ArrayList is obsolete class and List<Person> is a better choice for the collection.
One thing i didn't figure out is why we need to cast ArrayList to Generic List so we can access the properties of Person
ArrayList doesn't implement IEnumerable<Person> but IEnumerable only. It's possible that the ArrayList instance contians string, object, Form etc. items so we have either to filter out Person items via OfType<Person>() or cast items via Cast<Person>()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.