loc*_*boy 5 .net c# asp.net asp.net-cache
做出以下声明是否合法:
if(Cache[CACHE_KEY] == null)
{
//do something to form cache
}
else
{
//do something else that uses cache
}
Run Code Online (Sandbox Code Playgroud)
我不确定我的程序是否实际运行正常(即使它编译)并且我想知道缓存是否不存在它是否设置为null?
是的,这是合法的(但标题中的问题不是,详见下文).
但是,检查缓存中的类型是否是您期望的而不是必须两次执行此检查可能是明智的,例如:
//in English, the following line of code might read:
// if the item known in the cache by the specified key is in
// in fact of type MyExpectedReferenceType, then give me it
// as such otherwise, give me a null reference instead...
var myCachedInstance = Cache[key] as MyExpectedReferenceType;
if (myCachedInstance == null)
{
//we retrieved a reference to an instance of an MyExpectedReferenceType
}
else
{
//oh, no - we didn't!
}
Run Code Online (Sandbox Code Playgroud)
虽然重新阅读你的问题,并且考虑到你的计划不正常,我很想说你有比这更大的问题; 你的程序怎么不能正常工作?该Cache实例本身绝不会null同时访问-这是一个只读字段Page.但是,您的预期缓存值可能是null,如果这是问题,您应该收到NullReferenceException- 是这样吗?
更新:
要解决您的评论,请查看我添加到代码中的评论.
| 归档时间: |
|
| 查看次数: |
5101 次 |
| 最近记录: |