Correct Way to Add byte Variables into byte Arrays

六眼飞鱼酱① 提交于 2019-12-24 20:40:19

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!