-3

I have a function to get data from a network endpoint

public Byte[] GetData(string ip, int port ,Byte [] query, int responseLen)
{            
    Connection connection = GetConnection(ip,port);
    Byte[] data;
    try
    {
        data = connection.GetData(query, responseLen);
    }
    catch(IOException e)
    {
        //return an empty array
        data = new Byte[] { };
    }
    return data;
}

In case there is an exception from GetData function I am returning an empty array to the caller of GetData function.

I want to know how the caller can test if the byte array returned is empty or non-empty

liv2hak
  • 14,472
  • 53
  • 157
  • 270

2 Answers2

1

Don't forget about the (new) null propagation operator! The shortest check is

if (data?.Length > 0)
{
    //Data were returned
}
else
{
    //An error occurred
}

If you're on an older version of c# you can use Prabhath's answer.

John Wu
  • 50,556
  • 8
  • 44
  • 80
0
if (data != null  && data.Length > 0)

try this