How to convert List <byte> to byte [] in C #?

Is it possible to convert through a for loop?

Is there a better way to do this?

  for (int i = 0; i < myListByte.Count ;i++) { myArryByte[i] = myListByte[i]; } 
+6
source share
6 answers
 myArrayByte = myListByte.ToArray(); 
+16
source
 List<byte> bytes = ...; byte[] bArrary = bytes.ToArray(); 
+8
source

Use the List object toArray method .

+8
source
 byte[] arr = myListByte.ToArray(); 
+7
source

This may be what you are looking for:

 private void convertByteArray() { List<byte> byteList = new List<byte>() {2, 3, 4}; byte[] byteArray = byteList.ToArray<byte>(); } 
+2
source
  byte[] converted = myListByte.ToArray(); 
0
source

Source: https://habr.com/ru/post/888975/


All Articles