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!
To return a collection with repeated elements in C#, use Enumerable. Repeat method. It is part of System. Linq namespace.
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.
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.
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
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With