1

I have following field in my class:

public bool? EitherObject1OrObject2Exists => ((Object1 != null || Object2 != null) && !(Object1 != null && Object2 != null)) ? true : null;

But in Visual Studio I get an IntelliSense error that there cannot be a conversion between "bool" and "NULL" even though the field is nullable. What do I do wrong? And is there a cleaner way to check if either one of two objects is not null but one of them must be null?

Palmi
  • 2,381
  • 5
  • 28
  • 65

1 Answers1

5

try

? (bool?) true : null;

the problem is that default bool (true) is not nullable so the case statement are returning different types as far as the compiler is concerned

And you can remove the redundancy as pointed out by Servy

Object1 != null ^ Object2 != null
Steve
  • 11,696
  • 7
  • 43
  • 81