Using the KeyedCollection object in C# .NET
January 26, 2016 Leave a comment
The abstract generic KeyedCollection object can be used to declare which field of your custom object to use as a key in a Dictionary. It provides sort of a short-cut where you’d want to organise your objects in a Dictionary by an attribute of that object.
Let’s take the following object as an example:
public class CloudServer
{
public string CloudProvider { get; set; }
public string ImageId { get; set; }
public string Size { get; set; }
}
The Image IDs are always unique so the ImageId property seems to be a good candidate for a dictionary key.
Here’s an example: