How to create a 1-Dimensional Array in C# with index starting at 1

前端 未结 4 1368
花落未央
花落未央 2021-01-01 20:59

For multidimensional arrays Array.CreateInstance can be used to create non-zero index based arrays, but if you try that for a 1-dimensional arrays (vectors) as in e.g.:

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 21:54

    If anybody is still looking for a one-based array implementation, here is a one-dimensional generic one-based array, which is just a wrapper type on a regular array:

    https://github.com/ColmBhandal/CsharpExtras/blob/master/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs

    There is also a two-dimensional version, but no higher dimensional versions as of yet (this code was written to support Excel, so 2D was all that was needed):

    https://github.com/ColmBhandal/CsharpExtras/blob/master/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs

    Both of these types have test written for them in an associated test project under the same repo:

    https://github.com/ColmBhandal/CsharpExtras/tree/master/CsharpExtrasTest/OneBased

    Disclaimer: these repos are hosted on my personal GitHub account and are open source.

提交回复
热议问题