Skip to main content
removed the part that stemmed from a bad MSDN search...
Source Link
Mathieu Guindon
  • 75.6k
  • 18
  • 195
  • 469

HttpContext.Current.Cache.Items is an IDictionary, which means it would probably be safer to use IDictionary.Contains with the specified key. As is, your method will return false when the specified key does exist, but refers to a null reference (which should be valid) - that's a potential bug right there.

HttpContext.Current.Cache.Items is an IDictionary, which means it would probably be safer to use IDictionary.Contains with the specified key. As is, your method will return false when the specified key does exist, but refers to a null reference (which should be valid) - that's a potential bug right there.

removed misplaced quote block
Source Link
Mathieu Guindon
  • 75.6k
  • 18
  • 195
  • 469
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs.
/// </summary>
/// <typeparam name="T">Type of cached item (inferred from usage).</typeparam>
/// <param name="key">A string used to uniquely identify the added value.</param>
/// <param name="value">Item/value to be cached.</param>
public static void Add<T>(string key, T value)
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs.
/// </summary>
/// <typeparam name="T">Type of cached item (inferred from usage).</typeparam>
/// <param name="key">A string used to uniquely identify the added value.</param>
/// <param name="value">Item/value to be cached.</param>
public static void Add<T>(string key, T value)
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs.
/// </summary>
/// <typeparam name="T">Type of cached item (inferred from usage).</typeparam>
/// <param name="key">A string used to uniquely identify the added value.</param>
/// <param name="value">Item/value to be cached.</param>
public static void Add<T>(string key, T value)
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs.
/// </summary>
/// <typeparam name="T">Type of cached item (inferred from usage).</typeparam>
/// <param name="key">A string used to uniquely identify the added value.</param>
/// <param name="value">Item/value to be cached.</param>
public static void Add<T>(string key, T value)
filtered catch block
Source Link
Mathieu Guindon
  • 75.6k
  • 18
  • 195
  • 469
public static bool TryGet<T>(string key, out T value) 
{
    bool result;

    try
    {
        if (Exists(key))
        {
            value =  (T)HttpContext.Current.Cache[key];
            result = true;
        }
    }
    catch(InvalidCastException)
    {
        value = default(T);
    }

    return result;
}
public static bool TryGet<T>(string key, out T value) 
{
    bool result;

    try
    {
        if (Exists(key))
        {
            value =  (T)HttpContext.Current.Cache[key];
            result = true;
        }
    }
    catch
    {
        value = default(T);
    }

    return result;
}
public static bool TryGet<T>(string key, out T value) 
{
    bool result;

    try
    {
        if (Exists(key))
        {
            value =  (T)HttpContext.Current.Cache[key];
            result = true;
        }
    }
    catch(InvalidCastException)
    {
        value = default(T);
    }

    return result;
}
Source Link
Mathieu Guindon
  • 75.6k
  • 18
  • 195
  • 469
Loading