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)
An array can hold a collection of integers, floating-point numbers, stings or even other arrays. Stack. A stack stores a collection of items in the linear order that operations are applied. This order could be last in, first out (LIFO) or first in, first out (FIFO).
Basically, data structures are divided into two categories: Linear data structure. Non-linear data structure.
To store huge data donot use arrays where implementation is easy but insertion and deletion is very difficult. Hence use dynamic like linked list where insertion and deletion is easy. Implement your problem using linked list where the members of linked list are arrays of 100 characters size.
It sounds like your are looking for a Dictionary.
Dictionary<string, Person> peoples = new Dictionary<string, Person>();
Person oPerson = new Person("Julie", 23, 45);
peoples.Add(oPerson.Name, oPerson);
Another option is System.Collections.ObjectModel.KeyedCollection. This takes a little more work to implement, but can be useful.
To make this work, create a collection class for person and override the GetKeyForItem method:
public class PersonCollection : System.Collections.ObjectModel.KeyedCollection<string, Person>
{
protected override string GetKeyForItem(Person item)
{
return item.Name;
}
}
Then you can add items to the collection as in your example:
PersonCollection peoples = new PersonCollection();
peoples.Add(new Person("Julie", 23, 45));
Then to remove the item:
peoples.Remove("Julie");
Have a look at the KeyedCollection<TKey, TValue> Class.
KeyedCollection<TKey, TValue> Class
Provides the abstract base class for a collection whose keys are embedded in the values.
You need to derive your own collection class from this abstract class, e.g.
class PersonCollection : KeyedCollection<string, Person>
{
protected override string GetKeyForItem(Person item)
{
return item.Name;
}
}
Example:
static void Main(string[] args)
{
var peoples = new PersonCollection();
var julie = new Person("Julie", 23, 45)
peoples.Add(julie);
people.Remove(julie);
// - or -
people.Remove("Julie");
}
Note that the Name property of your Person class should be immutable (read-only).
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