Inside Arrays: Grounded at Zero

The introduction of .NET to VB required many changes in how VB worked: One of these changes was the removal of support for non-zero based arrays. Although .NET has a non-zero based single dimension array, it is a different type from the SZArray. If VB were to have supported non-zero-based arrays, it would have had to provide different syntax for SZArrays. Given the framework typically only uses and exposes SZArrays, it would also have meant that the uses for the non-zero based arrays would be limited. That's not to say a better solution wouldn't have been possible.

Rather than have a SZArray be a different type, the CLR team could have instead made all single dimension arrays the same type, and provided zero-based access to members alongside the access offset by the lower-bound. Then, the languages could choose to provide an implementation for either zero-based access, offset-based access, or both.

Unfortunately, that didn't happen, and the real value of adding potential complexities to the languages as they try to support both means of access is questionable. One reason people give for wanting non-zero based arrays include examples where data is stored against years. But in these cases, developers would probably be better off using a sparse array or generic Dictionary(Of TKey, TValue). The lack of support for non-zero based arrays does hit one group of people particularly hard: Those who like to think of arrays as having the first item being one. VB aided and nurtured a generation of developers with Option Base one.

The lack of one-based operations isn't limited to arrays; it's in all framework methods. Many of VB's methods on the other hand are one-based in an attempt to maintain compatibility with previous versions. This leads to a mismatch, such as IntStr being one-based, while String.IndexOf is zero-based. As you work with the framework, you are forced to think in terms of zero-based. Arrays cast as IList or the generic IList(Of T) lose all indication of lower-bound: Item can only be used practically if it's considered to be zero-based. The move from one-based to zero-based isn't a question of functionality; it's a question of perspective. A mix-and-match of one- and zero-based methods rapidly becomes confusing, and the chances of a coding slip up increase. There's a lot to be said for the simplicity of having everything zero-based.

comments powered by Disqus

Featured

  • VS Code 1.127 Further Integrates Advanced Browser-AI Tech

    Microsoft's July 1 Visual Studio Code update continues a recent push to make the editor's integrated browser a more capable development surface -- and a more useful tool for AI agents.

  • Support Vector Regression with SGD Training Using C#

    Support vector regression can predict numeric values effectively, and this article shows how to implement and train a kernel SVR model in C# using stochastic sub-gradient descent.

  • New GitHub Switch Limits Repo Issue Creation to Collaborators Only

    After publicly touting pull request limits as a way to cut maintainer noise, GitHub is taking the same idea further with a new setting that lets repository admins restrict issue creation to collaborators only.

  • Uno Platform Helps Ship First Stable SkiaSharp 4.0 Release for 2D .NET Graphics

    SkiaSharp 4.148.0 is the first stable v4 release, bringing a newer Skia engine, API cleanup, performance work and a Microsoft-Uno co-maintenance model.

Subscribe on YouTube