Search code examples
c#loopsvariablesrangesbyte

Creating 100 variables


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


Solution

  • 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.