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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16476267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:06:17  来源:igfitidea点击:

How to Check byte array empty or not?

c#

提问by SANDEEP

I am downloading the word file for GetSourceAttachmentmethod. When this method returns empty bytes then my byte Attachmentarray gives an error:

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

Object reference not set instance of object

对象引用未设置对象实例

It gives the error when I check the length of Attachmentin ifcondition.

它给人的错误,当我检查的长度Attachmentif条件。

Can any one help me to default initialize the byte array, then check the length?

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

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
{

}

采纳答案by Soner G?nül

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.

条件与运算符 (&&) 执行其布尔操作数的逻辑与,但仅在必要时评估其第二个操作数。

回答by Matthew Watson

You must swap the order of your test:

您必须交换测试的顺序:

From:

从:

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

To:

到:

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

The first version attempts to dereference Attachmentfirst and therefore throws if it's null. The second version will check for nullness first and only go on to check the length if it's not null (due to "boolean short-circuiting").

第一个版本尝试首先取消引用Attachment,因此如果它为空则抛出。第二个版本将首先检查空值,如果它不为空,则仅继续检查长度(由于“布尔短路”)。

回答by Habib

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

首先检查附件是否为空然后是长度,因为您正在使用&&它会导致短路评估

&& Operator (C# Reference)

&& 运算符(C# 参考)

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

条件与运算符 (&&) 对其 bool 操作数执行逻辑与操作,但仅在必要时评估其第二个操作数

Previously you had the condition like: (Attachment.Length > 0 && Attachment != null), since the first condition is accessing the property Lengthand if Attachmentis 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 Attachmentis not null.

以前你有这样的条件:(Attachment.Length > 0 && Attachment != null),因为第一个条件是访问属性Length,如果Attachment为空,你最终会出现异常,使用修改后的条件(Attachment != null && Attachment.Length > 0),它将首先检查空值,如果Attachment不是空值,它只会进一步移动。

回答by Metin Atalay

.Net V 4.6 OR C # 6.0

.Net V 4.6 或 C# 6.0

Try This

尝试这个

 if (Attachment?.Length > 0)

回答by jbdeguzman

Now we could also use:

现在我们还可以使用:

if (Attachment != null  && Attachment.Any())

Any() is often easier to understand in a glance for the developer than checking Length() > 0. Also has very little difference with processing speed.

对于开发人员来说,Any() 通常比检查 Length() > 0 更容易一目了然。而且与处理速度的差异也很小。

回答by Ganesan J

In Android Studio version 3.4.1

在 Android Studio 版本中 3.4.1

if(Attachment != null)
{
   code here ...
}