-3

So I created an array with 100 variables using Enumerable.Range. The data type is limited to Int32.

Problem

  • How can I create the same array with SByte?

  • Am I right in thinking I would need to use a loop to create and index the variables?

I have looked around online and most results touch on declaring counting variables for the loop but not using a loop to declare variables

  • 2
    _"So I created an array with 100 variables using enumerable range [...] How can I create the same array with sByte?"_ Well... exactly the same way, but change `int` to `sbyte`. – RobIII Apr 28 '21 at 08:46
  • 3
    You wouldn't use a loop to declare variables. You can use a loop to add elements to an array. Maybe the mix-up in terminology is causing problems with your searching? – canton7 Apr 28 '21 at 08:47
  • 2
    `Enumerable.Range(1, 100).Select(n => (sbyte)n).ToArray()`. But this does not create 100 *variables*, but an array of 100 *values*. – Klaus Gütter Apr 28 '21 at 08:50

1 Answers1

1

Just cast them:

SByte[] array = Enumerable.Range(0, 100).Select(i => (SByte) i).ToArray();

note that SByte is not cls compliant, you might want to use short instead.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939