I'm working with a library which contains some 'cacheHelper' functions which act as a wrapper for bits of the System.Runtime.Caching namespace.
For example:
public bool IsInCache(string keyname){
    return MemoryCache[keyname] != null;
}
public static void SaveToCache(string cacheKey, object savedItem, 
                               DateTime absoluteExpiration)
{
    var policy = new CacheItemPolicy
    {
        AbsoluteExpiration = absoluteExpiration,
        RemovedCallback = CacheEntryRemovedCallback
    };
    MemoryCache.Default.Add(cacheKey, savedItem, policy);
}
All fairly standard stuff. We also have a method to retrieve cached objects:
public static T GetFromCache<T>(string cacheKey) where T : class
{
    return MemoryCache.Default[cacheKey] as T;
}
I found that if I save an object to the cache as an object of type X and then try and retrieve it from the cache, mistakenly, as an object of type Y, MemoryCache.Default[cachekey] returns null and no exception is thrown. I was expecting something like an InvalidCastException. Can anyone explain why?
 
    