Pete
Pete

Reputation: 58422

Check if StringBuilder is null

I'm try to check if a StringBuilder is null after I have tried to populate it from the cache:

StringBuilder videoSitemap;

if (AppSettings.CachingEnabled)
{
    videoSitemap = CacheHelper.Get<StringBuilder>("DynamicVideoSitemap");
}

if (videoSitemap == null)
{
    videoSitemap = new StringBuilder();
    ....
}

But I am getting the following error when trying to compile:

CS0165 Use of unassigned variable 'videoSitemap'

How do I do this and then check if the object is null - if I instantiate it but don't use caching then the StringBuilder will never be null

Upvotes: 1

Views: 796

Answers (1)

Jenish Rabadiya
Jenish Rabadiya

Reputation: 6766

You need to assign default null to avoid this error.

StringBuilder videoSitemap = null;

Upvotes: 8

Related Questions