Runtime Storage in C#

Sometimes you just want to store some random value. For this I wrote the RuntimeStorage class. It serves me in some games as a naive cache for a couple of less important values.

It can store integer, float and string values. Thanks to overloading there are actually only two different methods: Get and Set. For Get, it is always necessary to assign a default value (otherwise the overloading will not work). This default value is returned if no stored value was found.

using System.Collections.Generic;

public class RuntimeStorage
{
    private Dictionary<string, int> _integerValues = new Dictionary<string, int>();
    private Dictionary<string, float> _floatValues = new Dictionary<string, float>();
    private Dictionary<string, string> _stringValues = new Dictionary<string, string>();

    public void Set(string key, int value)
    {
        _integerValues[key] = value;
    }

    public void Set(string key, float value)
    {
        _floatValues[key] = value;
    }

    public void Set(string key, string value)
    {
        _stringValues[key] = value;
    }

    public int Get(string key, int defaultValue)
    {
        int value;
        if (_integerValues.TryGetValue(key, out value)) {
            return value;
        }

        return defaultValue;
    }

    public float Get(string key, float defaultValue)
    {
        float value;
        if (_floatValues.TryGetValue(key, out value)) {
            return value;
        }

        return defaultValue;
    }

    public string Get(string key, string defaultValue)
    {
        string value;
        if (_stringValues.TryGetValue(key, out value)) {
            return value;
        }

        return defaultValue;
    }
}

This is how you store values:

RuntimeStorage myStorage = new RuntimeStorage();
myStorage.Set("someInteger", 42);
myStorage.Set("someFloat", 0.815);
myStorage.Set("someString", "foo");

And this is how you retrieve them:

RuntimeStorage myStorage = new RuntimeStorage();
myStorage.Get("someInteger", 0);
myStorage.Get("someFloat", 0.0f);
myStorage.Get("someString", "");

By the way, you can also make the whole thing a Singleton and thus share values between different classes.