Well, a null coalescing operator can help with the first:
if (AddressLine1 ?? AddressLine2 ?? Town ?? Country != null) { if (AddressLine1 == null || AddressLine2 == null || Town == null || Country == null) { return false; }
You might want to write some helper methods:
if (IsAnyNonNull(AddressLine1, AddressLine2, Town, Country)) { if (IsAnyNull(AddressLine1, AddressLine2, Town, Country)) { return false; } }
If the utility methods look something like this:
public static bool IsAnyNonNull(params object[] values) { return values.Any(x => x != null); } public static bool IsAnyNull(params object[] values) { return values.Any(x => x == null); }
Of course, you still have two if , but I think that this is basically necessary here.
source share