ConcurrentDictionary, Store Key and Compression So i have a  ConcurrentDictionary&lt;int, string&gt;

The "int" is merely the way i store the key, which i want more recommendations on.

Currently i do like this.

        public string memoize(Func&lt;string, uint, string&gt; functor, string code, uint id)
    {
        //Used for the lookup to identify if it's been inputed before
        int codeHash = code.GetHashCode();
        string functor_return;


        if (_compilerCache.TryGetValue(codeHash, out functor_return))
            return functor_return;


        functor_return = functor?.Invoke(code, id);
        _compilerCache[codeHash] = functor_return;

        return functor_return;
    }


Why do i use GetHashCode()?
I don't know, it's just the only way i could figure out rather than store the string value itself.

I also save this "Cache", and hence why the compression and key is important.

    //Serialize and Compress object to file
    public static void SerializeObject&lt;T&gt;(string filename, T obj)
    {
        using (Stream stream = File.Open(filename, FileMode.Create))
        using (var cStream = new GZipStream(stream, CompressionLevel.Optimal))
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(cStream, obj);
        }
    }

    //DeSerialize and Decompress object from file
    public static T DeSerializeObject&lt;T&gt;(string filename)
    {
        T objectToBeDeSerialized = default(T);
        try
        {
            using (Stream stream = File.Open(filename, FileMode.Open))
            using (var cStream = new GZipStream(stream, CompressionMode.Decompress))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                objectToBeDeSerialized = (T)binaryFormatter.Deserialize(cStream);
            }

        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message + " : Error DeSerializing Cache, corrupted?");
        }

        return objectToBeDeSerialized;
    }


It all works, but i would like to improve it.
I don't really know if even GetHashCode is a good way to do this thing,
i just took it randomly as before i just stored the value itself.

The values are usually around 10-20 characters i guess, usually very similar (which should be compression friendly i guess).

So can anything be done to improve this?
GetHashCode is used internally by ConcurrentDictionary to speed up access to the real key.

But, there is no guarantee that 2 different strings will return 2 different HashCode, you may encounter collisions! Such collisions are handled by the ConcurrentDictionary. 

So with your code, as the _compilerCache is a Dictionary&lt;int,functor&gt; (it seems to be) and not a Dictionary&lt;string,functor&gt;, you may encounter rare events where two different codes will give the same key. If such a collision happends, you have a subtle and rare bug.

I suggest the following :    


stick to a Dictionary&lt;string,functor&gt;, use the real key
don't input a HashCode instead of the real key 
you may also input your own GetHashCode in Dictionary constructor


Using code instead of int as a key has a price : memory footprint is larger, but with small strings (10-20 char), it should not be an issue.

Hope this helps !