[英]Is this the best way to convert multi dimensional array of Byte to List of Byte-arrays?
我有一個必須返回字節列表的方法,如下所示:
public List<byte[]> ExportXYZ(IPAddress ipAddress, WebCredential cred)
在上述方法中,我正在調用第三方方法,該方法返回多維字節數組:
byte[][] tempBytes = xyzProxy.ExportCertificates();
所以現在我需要將byte[][]
轉換為List<byte[]>
。
我寫了下面的代碼
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;
}
我覺得這不是編碼的好方法。 誰能幫我寫出正確的轉換代碼?
您的方法不返回byte
列表-它返回字節數組的byte[]
列表。
由於您的輸入是字節數組的數組,因此您可以簡單地從一個轉換為另一個。
使用LINQ:
byte[][] arr = ...;
List<byte[]> list = arr.ToList();
沒有LINQ:
byte[][] arr = ...;
List<byte[]> list = new List<byte[]>(arr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.