Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieval of items from custom collection

Tags:

c#

collections

I have a following class

public class People
{
    public int id;
    public string nameHash;
    public string name;
}

I need to create a custom collection, consisting of objects of class People, that lets me retrieve elements by its id and nameHash. The collection must have the ability to iterate through its elements using foreach:

foreach (People person in PeopleCollection) { ... }

How do I do that? If you can not give a detailed answer, at least give a brief plan of action. Thanks in advance!

like image 842
Max Avatar asked Mar 21 '11 18:03

Max


People also ask

How do I return a collection in C#?

To return a collection with repeated elements in C#, use Enumerable. Repeat method. It is part of System. Linq namespace.

Should I use collection or list C#?

The reason "why" you "should" use a Collection<T> instead of a List<T> is because if you expose a List<T> , then anyone who gets access to your object can modify the items in the list. Whereas Collection<T> is supposed to indicate that you are making your own "Add", "Remove", etc methods.

What is list collection in C#?

List in C# is a collection of strongly typed objects. These objects can be easily accessed using their respective index. Index calling gives the flexibility to sort, search, and modify lists if required. In simple, List in C# is the generic version of the ArrayList. This ArrayList comes under System.


2 Answers

Is there a specific reason why it has to be a custom collection? Why not

List<People> PeopleCollection = new List<People>();

you can retrieve elements using id and nameHash and you can iterate over PeopleCollection

like image 143
Bala R Avatar answered Nov 05 '22 17:11

Bala R


If you're talking about a large collection and you want faster lookups based on an integer Id or a string NameHash field while still supporting the foreach (Foo f in fooCollection) pattern, then you can roll your own collection that wraps a pair of dictionaries. Crude implementation, not thoroughly tested:

class Person
{
    public int Id { get; private set; }
    public string NameHash { get; private set; }
    public string Name { get; private set; }

    public Person(int id, string nameHash, string name)
    {
        Id = id;
        NameHash = nameHash;
        Name = name;
    }
}

class People : IEnumerable<Person>
{
    private Dictionary<int, Person> personDictionary = new Dictionary<int, Person>();
    private Dictionary<string, int> hashIdMap = new Dictionary<string, int>();

    public void Add(Person person)
    {
        if (person == null)
            throw new ArgumentNullException("person");

        if (personDictionary.ContainsKey(person.Id))
            throw new InvalidOperationException("person Id is already referenced in collection.");

        if (hashIdMap.ContainsKey(person.NameHash))
            throw new InvalidOperationException("person NameHash is already referenced in collection.");

        personDictionary.Add(person.Id, person);
        hashIdMap.Add(person.NameHash, person.Id);
    }

    public Person this[int id]
    {
        get
        {
            if (!personDictionary.ContainsKey(id))
                throw new ArgumentOutOfRangeException("Id is not in the collection.");

            return personDictionary[id];
        }
    }

    public Person this[string nameHash]
    {
        get
        {
            if (!hashIdMap.ContainsKey(nameHash))
                throw new ArgumentOutOfRangeException("NameHash is not in the collection.");

            return this[hashIdMap[nameHash]];
        }
    }

    public IEnumerator<Person> GetEnumerator()
    {
        foreach (KeyValuePair<int, Person> pair in personDictionary)
            yield return pair.Value;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

...

static void Main()
{
    Person personA = new Person(1, "A", "Apple");
    Person personB = new Person(2, "B", "Banana");
    Person personC = new Person(3, "C", "Cherry");

    People people = new People();
    people.Add(personA);
    people.Add(personB);
    people.Add(personC);

    Person foo = people[1];
    Person bar = people["C"];

    Debug.Assert(foo.Name == "Apple");
    Debug.Assert(bar.Name == "Cherry");

    foreach (Person person in people)
        Debug.WriteLine(person.Name);
}

Of course, if you're dealing with a small-ish collection, you could very well simply use a List<T> and utilize either LINQ or the Find methods already defined. Such as

Person personA = collection.FirstOrDefault(p => p.Id == 42);
Person personB = collection.Find(p => p.NameHash == "Blah");
like image 22
Anthony Pegram Avatar answered Nov 05 '22 17:11

Anthony Pegram