正确的方法为字节变量加入到字节数组 [英] Correct Way to Add byte Variables into byte Arrays

查看:139
本文介绍了正确的方法为字节变量加入到字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是从用户获取输入并将其存储在一个字节数组。请注意,我必须VAR1变量存储在一个字节数组,而不是一个列表。

  Console.Write(输入一个数字:);
字节VAR1 = byte.Parse(到Console.ReadLine());
字节[]的字节数组=新的字节[] {};
的字节数组[0] = VAR1;


解决方案

数组大小是固定的,你必须在创建它的specifiy数组的大小。在你的榜样,你告诉它通过把使大小为0的数组的 {} 后的byte [] 。相反,删除 {} 键,只放了1之间的 []

  Console.Write(输入一个数字:);
字节VAR1 = byte.Parse(到Console.ReadLine());
字节[]的字节数组=新的字节[1];
的字节数组[0] = VAR1;

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;

解决方案

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;

这篇关于正确的方法为字节变量加入到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆