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:
use nullable types
var Buffer = new byte?[1000];
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;
}
精彩评论