开发者

How to find empty bytes array

How to find out is bytes array has any data or it 开发者_运维问答is newly created bytes array?

var Buffer = new byte[1000];
//How to find out is Buffer is empty or not?


I assume by 'empty' you mean containing default values for every byte element, if this isn't what you mean, look at @sehe's answer.

How about using LINQ to check whether all elements have the default value for the type:

var Empty = Buffer.All(B => B == default(Byte));


A byte is a valuetype, it cannot be null;

Creating an array immediately initializes the elements to the default value for the element type.

This means, empty cells can't exist, let alone be detected.

If you must:

  1. use nullable types

    var Buffer = new byte?[1000];

  2. use Array.Resize when capacity changes. However, you could soon be in a situation where just using a System.Collections.Generic.List would be much more efficient


In addition to the answers given

            var buffer = new byte[1000];
            var bFree = true;
            foreach (var b in buffer)
            {
                if (b == default(byte)) continue;
                bFree = false;
                break;
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜