0

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?
4
  • Isn't there a function like Buffer.isEmpty()? Commented Jun 30, 2011 at 9:25
  • what do you mean by empty ? full of zero ? Commented Jun 30, 2011 at 9:25
  • c# do not provide such function. Commented Jun 30, 2011 at 9:26
  • Buffer is not a keyword under System? we can declare keyword as variable? Commented Mar 7, 2014 at 4:34

3 Answers 3

6

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));
Sign up to request clarification or add additional context in comments.

4 Comments

default(T) would only have a meaning in the context of a template, IIRC
@sehe: Nope, it's a perfectly valid operator regardless of generics.
The .All() method on System.Buffer does not exist. Even if I include System.Linq.
@Robula I'm referring to the buffer variable in the question, rather than system.buffer
0

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

Comments

0

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;
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.