简体   繁体   English

这是将字节的多维数组转换为字节数组列表的最佳方法吗?

[英]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[]> . 所以现在我需要将byte[][]转换为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? 谁能帮我写出正确的转换代码?

Your method doesn't return a list of byte - it returns a list of byte[] , of byte arrays. 您的方法不返回byte列表-它返回字节数组的byte[]列表。

Since your input is an array of byte arrays, you can probably simply convert from one to the other. 由于您的输入是字节数组的数组,因此您可以简单地从一个转换为另一个。

With LINQ: 使用LINQ:

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

Without LINQ: 没有LINQ:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM