Deepak Someshwara
Deepak Someshwara

Reputation: 5

Is this the best way to convert multi dimensional array of Byte to List of Byte-arrays?

I am having a method which must return List of Bytes as follow:

 public List<byte[]> ExportXYZ(IPAddress ipAddress, WebCredential cred)

Inside above method I am calling third party method which returns a multi-dimensional byte array:

 byte[][] tempBytes = xyzProxy.ExportCertificates();

So now I need to convert byte[][] to List<byte[]>.

I wrote below code

private List<byte[]> ConvertToSigleDimensional(byte[][] source)
{
    List<byte[]> listBytes = null;

    for(int item=0;item < source.Length;item++)
    {
        listBytes.Add(source[i][0]);
    }

    return listBytes;
}

I feel this is not a good way of coding. Can anyone help me to write proper code for the conversion?

Upvotes: 0

Views: 146

Answers (1)

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

Your method doesn't return a list of byte - it returns a list of byte[], of byte arrays.

Since your input is an array of byte arrays, you can probably simply convert from one to the other.

With LINQ:

  byte[][] arr = ...;
  List<byte[]> list = arr.ToList();

Without LINQ:

  byte[][] arr = ...;
  List<byte[]> list = new List<byte[]>(arr);

Upvotes: 4

Related Questions