chrishey
chrishey

Reputation: 451

Check for a null reference on HTTP cache

I am using the HTTP runtime cache on a project and have implemented a check to make sure it is not null before calling an operation on the instance.

I am not sure if this is required however, as you can see below the Cache property on the class is static. Does this mean that the Cache object would be instantiated as soon as I tried to use it (assuming it wasn't already) and therefore never throw a null reference exception?

public sealed class HttpRuntime
{
    public static Cache Cache { get; }
}

However to not check feels wrong in terms of defensively programming my application to recover from null references, especially as I follow that practice elsewhere.

Thanks

Upvotes: 1

Views: 115

Answers (1)

Martin Wedvich
Martin Wedvich

Reputation: 2266

The Cache object's lifespan is tied to the ASP.NET application, and it's instantiated whenever the application starts or restarts, so there's no need to null check it.

Upvotes: 1

Related Questions