SANDEEP SANDEEP - 11 months ago 136
C# Question

How to Check byte array empty or not?

here i m downloading the word file for GetSourceAttachment method.when this method returning empty bytes then my byte Attachment array gives an error(Object reference not set instance of object).when i m checking the length of Attachment in if condition then its giving the error. can any one help me to default initialize the byte array then check the length.

byte[] Attachment = null ;

string Extension = string.Empty;
ClsPortalManager objPortalManager = new ClsPortalManager();
Attachment = objPortalManager.GetSourceAttachment(Convert.ToInt32(hdnSourceId.Value), out Extension);
if (Attachment.Length > 0 && Attachment != null)
DownloadAttachment("Attacment", Attachment, Extension);
ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('Attachment is not Uploaded !');</script>");


Answer Source

Just do

if (Attachment != null  && Attachment.Length > 0)

From && Operator

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.