I have a Dictionary and would like to expose the member as read only. I see that I can return it as a IReadOnlyDictionar
It would be as easy as casting the whole dictionary reference to IReadOnlyDictionary because Dictionary implements IReadOnlyDictionary.
BTW, you can't do that because you want the List values as IReadOnlyList.
So you need something like this:
var readOnlyDict = (IReadOnlyDictionary>)dict.ToDictionary(pair => pair.Key, pair => pair.Value.AsReadOnly());
This is just a suggestion, but if you're looking for immutable dictionaries, add System.Collections.Immutable NuGet package to your solution and you'll be able to use them:
// ImmutableDictionary>
var immutableDict = dict.ToImmutableDictionary(pair => pair.Key, pair => pair.Value.ToImmutableList());
Learn more about Immutable Collections here.