For example, I have in my application a list of a type what has a persons name as it’s name and holds two values. The name of the type is the persons name and the type contains only their age and number of std’s.
My first idea was to make a class of Persons with Age and NumStds properties where Age and NumStds is required in the constructor and the create a List which I can add to.
class Person
{
public string Name { get; set; }
public int NumSTDs { get; set; }
public int Age { get; set; }
public Person(string name, int age, int stds)
{
Name = name;
Age = age;
NumSTDs = stds;
}
}
static void Main(string[] args)
{
List<Person> peoples = new List<Person>();
peoples.Add(new Person("Julie", 23, 45));
}
I was just wondering if there is a data structure where I could just refer to the elements in the List<> by their Name and have the properties attached to them come along for the ride. Like I could say
people.Remove(Julie)
Have a look at the KeyedCollection<TKey, TValue> Class.
You need to derive your own collection class from this abstract class, e.g.
Example:
Note that the Name property of your Person class should be immutable (read-only).