gpt4 book ai didi

c# - 如何检查字节数组是否为空?

转载 作者:IT王子 更新时间:2023-10-29 04:04:02 28 4
gpt4 key购买 nike

我正在为 GetSourceAttachment 方法下载 word 文件。当此方法返回空字节时,我的字节 Attachment 数组会出错:

Object reference not set instance of object

当我在 if 条件下检查 Attachment 的长度时出现错误。

谁能帮我默认初始化字节数组,然后检查长度?

try
{
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);
}
else
{
ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('Attachment is not Uploaded !');</script>");
}
}
catch
{

}

最佳答案

就这样

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

来自 && Operator

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

关于c# - 如何检查字节数组是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16476267/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com