Search code examples
c#.netsocketstcp

How to check if byte[] contains only zeros?


How can I check if a byte[] contains only 0's? I do not want to send the array over the network if it only contains zeroes:

byte[] bytesToBeSend = e.GetAudioSamples;

// Send test data to the remote device.
Send(client, bytesToBeSend);

Solution

  • Insert this test before your Send request and use an if test:

    bool hasAllZeroes = bytesToBeSend.All(singleByte => singleByte == 0);
    
    if (!hasAllZeroes) {
        Send(client, bytesToBeSend);
    }
    

    Make sure that you've included LINQ:

    using System.Linq;