I would like to check if an object is defined or exists using C#.
Something lik开发者_StackOverflow中文版e this:
if (defined(Object)){
}
You check if it's null
in C# like this:
if(MyObject != null) {
//do something
}
If you want to check against default (tough to understand the question on the info given) check:
if(MyObject != default(MyObject)) {
//do something
}
If a class type is not defined, you'll get a compiler error if you try to use the class, so in that sense you should have to check.
If you have an instance, and you want to ensure it's not null, simply check for null:
if (value != null)
{
// it's not null.
}
精彩评论