.NET 强制类型转换对性能影响大不?


经常会用到asp.net的缓存

System.Web.Caching.Cache

只知道它能缓存 object 类型

但是不知道具体类型的性能如何

如果缓存一个 List 的类型 ,如下代码
var keylist = GetCacheKeyDataList();

如果频繁的操作这个keylist 会不会影响性能?


 List<string> GetCacheKeyDataList()
    {
        List<string> datalist = null;

        string key = CACHE_KEY + "_KEYS";

        object cacheObject = HttpContext.Current.Cache.Get(key);

        if (cacheObject == null)
        {
            datalist = new List<string>(this.capacity);

            HttpContext.Current.Cache.Insert(key, datalist);
        }
        else
        {
            datalist = (List<string>)cacheObject;
        }

        return datalist;
    }

.net 缓存

西园寺欣桐 11 years ago

类型转换肯定有性能损失的,但是这个和你重新获取数据库的数据来比 ,仍然有巨大的优势。。

msgybs answered 11 years ago

可以使用 as 操作符

小吴酱SAMA answered 11 years ago

Your Answer