We have a class with a dictionary that’s public:
public class SomethingWithADictionary {
public Dictionary<string, Instance> Instances { get; set; }
}
Currently we access this dictionary directly, like this:
Instance inst = a.Instances["key"];
We want to make the dictionary private, but have a public way to get to the dictionary elements using the same indexer syntax. The reason is, if the instance is not in the dictionary, we’d like to take some action instead of just throwing an error.
How do you do that?
Does it have to be exactly the same syntax? If you don’t mind accessing it as:
then it’s easy – you just add an indexer: