I'm wondering if I can create a Property that returns a read-only Dictionary?
Example:
private readonly Dictionary<string, IMyObject> _myDictionary; public Dictionary<string, IMyObject> MyDictionary { get { return _myDictionary; } }
Therefore people using MyDictionary are not allowed to add, remove, or change items. Any way in which this can be done?
I think that you'll need a class that wraps a Dictionary
like the ReadOnlyCollection
wraps a List
.
While you will not find a default class that does this, you'll find an implementation in one of the answers to this question.
The BCL Extras Project also contains such an implementation. It supports the creation of a proxy object which implements IDictionary
and can be used in its place.
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