I have a Dictionary
and would like to expose the member as read only. I see that I can return it as a IReadOnlyDictionar
If you want to return a read only dictionary but still be able to mutate the dictionary and list in your class you could use casting to get back the list type.
This example is a bit contrived, but shows how it could work.
public class MyClass
{
Dictionary> _dictionary;
public IReadOnlyDictionary> Dictionary { get { return _dictionary; } }
public MyClass()
{
_dictionary = new Dictionary>();
}
public void AddItem(string item)
{
IReadOnlyList readOnlyList = null;
List list = null;
if (!_dictionary.TryGetValue(item, out readOnlyList))
{
list = new List();
_dictionary.Add(item, list);
}
else
list = readOnlyList as List;
list.Add(item);
}
}
If you goal is to have the property be immutable, then using a ReadOnlyDictionary would be the best option.