Nullable type issue with ?: Conditional Operator

Consider if you want to use if and only if condition (IIF) with a nullable type

DateTime? foo;
foo = true ? null : new DateTime(0);

This will throw a compiler error. To resolve this cast null to nullable type as below-

foo = true ? (DateTime?)null : new DateTime(0);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s