ZigiZen
ZigiZen

Reputation: 61

Shorter null check

Can I shorten something like this

if (variable != null) 
{
   Function();
}

to something like this

variable? Function();

but that actually works ?

Upvotes: 1

Views: 85

Answers (1)

div
div

Reputation: 925

No, you can't.

But if Function() is a member of variable you can use the null-conditional operator for the same effect:

variable?.Function();

Upvotes: 5

Related Questions