问题
What I am trying to do is get input from the user and store it in a byte array. Please note, I must store var1 variable in a byte array and not a list.
Console.Write("Enter a number: ");
byte var1 = byte.Parse(Console.ReadLine());
byte[] byteArray = new byte[] {};
byteArray[0] = var1;
回答1:
Arrays are fixed in size, you must specifiy the size of the array when you create it. In your example you told it to make an array of size 0 by putting the {}
after the byte[]
. Instead remove the {}
and just put a 1 between the []
Console.Write("Enter a number: ");
byte var1 = byte.Parse(Console.ReadLine());
byte[] byteArray = new byte[1];
byteArray[0] = var1;
来源:https://stackoverflow.com/questions/35927470/correct-way-to-add-byte-variables-into-byte-arrays