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

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube