Question: I have a public dictionnary within a public class as follow:

```
namespace ApiAssembly
{
    public static class TypeStore
    {
        /// <summary>
        /// Initializes static members of the <see cref="TypeStore"/> class.
        /// </summary>
        static TypeStore()
        {
            Store = new Dictionary<string, Type>();
        }

        /// <summary>
        /// Gets the store.
        /// </summary>
        public static Dictionary<string, Type> Store { get; }

        public void AddTypes()
        {
            // This should be allowed
            TypeStore.Store.Add("object", typeof(object));
        }
    }
}

```

I want to prevent adding new elements to this dictionnary except internally (managed through API). What is the best way to achieve this?

```
namespace ClientAssembly
{
    using ApiAssembly;

    public class Client
    {
        public void AddTypes()
        {
            // How to prevent this call?
            TypeStore.Store.Add("object", typeof(object));
        }
    }
}

```

The content of the Dictionnary must be publicly accessible, so just flipping the access modifier is not an option

Answer: 
Expose it as:

```
IReadOnlyDictionary<string, Type> dictionary = new Dictionary<string, Type>();

```

Or additionally use `ReadOnlyDictionary` wrapper to prevent casting back to `Dictionary`.

Full example:

```
public static class TypeStore
{
    private static Dictionary<string, Type> store;

    private static ReadOnlyDictionary<string, Type> storeReadOnly ;

    /// <summary>
    /// Initializes static members of the <see cref="TypeStore"/> class.
    /// </summary>
    static TypeStore()
    {
        store = new Dictionary<string, Type>();
        storeReadOnly = new ReadOnlyDictionary<string, Type>(store);
    }

    /// <summary>
    /// Gets the store.
    /// </summary>
    public static IReadOnlyDictionary<string, Type> Store => storeReadOnly;

    public static void AddTypes()
    {
        // This should be allowed
        TypeStore.store.Add("object", typeof(object));
    }
}

```
You should separate the actual storage dictionary and the one you use for the outside world. An easy way would be:

```
private static Dictionary<string, Type> Storage { get; } = new Dictionary<string, Type>();

public static ReadOnlyDictionary<string, Type> Store 
              => new ReadOnlyDictionary<string, Type>(Storage);

```

Where `Storage` is the actual backing dictionary you can edit.

Or even better, expose the methods you want to have available through your class (it serves as a proxy), where you never grant external classes access to the dictionary itself.