Your check should be:
if (Attachment != null && Attachment.Length > 0)
First check if the Attachment is null and then lenght, since you are using &&
that will cause short-circut evaluation
The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.
Previously you had the condition like: (Attachment.Length > 0 && Attachment != null)
, since the first condition is accessing the property Length
and if Attachment
is null, you end up with the exception, With the modified condition (Attachment != null && Attachment.Length > 0)
, it will check for null first and only moves further if Attachment
is not null.