MetaGuru
MetaGuru

Reputation: 43833

Is there a way to check an object reference to see if it is not set to an instance of an object?

The compiler is always complaining about: "Object reference not set to an instance of an object." So hopefully there is an easy way to check whether this is the case before I try doing something that would cause it... thanks.

Upvotes: 0

Views: 271

Answers (1)

Noon Silk
Noon Silk

Reputation: 55082

Heh. Yes. Use

if(obj != null){
}

If it's a string, it's generally good to use:

if( !string.IsNullOrEmpty(foo) ){
}

Upvotes: 3

Related Questions