-2

I'm adding value to an array of bytes with the following code:

byte[] ConnectionPath;


ConnectionPath[0] = 0;
ConnectionPath[1] = 2;
ConnectionPath[2] = 1;
ConnectionPath[3] = 0;

My question is, can't I do this in just 1 line of code? I tried this, but this doesn't work. (I know that you can do this by declaration, but of course this value changes through the program)

ConnectionPath = { 0, 2, 1, 0};
Belekz
  • 480
  • 1
  • 7
  • 18
  • 3
    `byte[] ConnectionPath = { 0, 2, 1, 0};` does work, but you need to do it at the point of declaration – UnholySheep Jan 16 '18 at 14:01
  • 3
    If you need to split declaration and initialization then `ConnectionPath = new byte[]{ 0, 2, 1, 0};` works – UnholySheep Jan 16 '18 at 14:03
  • I know that you can do it at the point of declaration, but that is useless in my application, because the values changes a couple of times during the program. – Belekz Jan 16 '18 at 14:04

1 Answers1

3

If you do it all in one line this works:

byte[] ConnectionPath = { 0, 2, 1, 0 };

otherwise you have to tell the compiler what type of array it is:

byte[] ConnectionPath;
ConnectionPath = new byte[]{ 0, 2, 1, 0 };
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939