Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unchangeable Dictionary

Tags:

c#

.net

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?

like image 306
Carra Avatar asked Dec 30 '22 08:12

Carra


1 Answers

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.

like image 120
luvieere Avatar answered Dec 31 '22 22:12

luvieere