Is it legal to make the following statement:
if(Cache[CACHE_KEY] == null)
{
//do something to form cache
}
else
{
//do something else that uses cache
}
I'm not sure my program is actually functioning properly (even though it compiles) and am wondering if a cache doesn't exist is it set to null?
Yes, that is legal (but the question in the title is not, see below for details).
Though, it may be wise to check that the type in the cache is what you're expecting rather than having to do this check twice, such as:
On re-reading your question though, and thinking about your program not working properly, I'm tempted to say you have bigger issues than this; how is your program not working correctly? The
Cache
instance itself will never benull
while accessible - it is a read-only field ofPage
. However, your expected cached value could benull
and, if this is the problem, you should be receiving aNullReferenceException
- is that the case?UPDATE:
To address your comment, check out the comments I added to the code.