I just want to know what you guys think of the following code part and if you think it's readable or if you'd do it differentely.
Let's assume we have this class:
public class Foo
{
public bool IsFooBar { get; set; }
}
An now we want to check for IsFooBar
, but are not sure if our instance of Foo
is null
:
internal class Program
{
private static void Main(string[] args)
{
Foo foo = GetFoo();
bool secondCondition = GetSecondCondition();
if ((foo?.IsFooBar ?? false) && secondCondition) //relevant line
{
//true
}
}
private static Foo GetFoo()
{
//clever logic is here, CAN return null
return null;
}
private static bool GetSecondCondition()
{
//even mor clever logic
return true;
}
}
What do think about the relevant line? Is this code nice? Or would you rather write:
if (foo != null && foo.IsFooBar && secondCondition)
Or maybe you guys even got a better idea.