BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Collection Initializers in VB

| by Jonathan Allen Follow 132 Followers on May 07, 2008. Estimated reading time: 1 minute |

Collection initializers were supposed to be released along with LINQ in C# 3 and VB 9. While C# did get them, they were cut from the VB release. Part of the reason was the Visual Basic team wanted to make VB's version more powerful.

In C#, collection initializers are limited to explicitly known types. For VB, Microsoft wants to also support inferred types. This would allow developers to write statements such as:

Dim x = {1, 2, 3, 4}
Dim y = {{"a", 1}, {"b", 2}, {"c", 3}, {"d", 4}}
Dim z = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}

There is, however, some debate on exactly what this syntax should mean. Some argue that in all cases it should return arrays, while others want to see lists and dictionaries. Part of this stems from the fact that VB does not have a clean syntax for quickly defining tuples. Tuples are increasing becoming important as we move towards more functional programming and dynamic typing.

Anthony Green brings up the issue of jagged vs. rectangular arrays. In our example, variable z could be either Integer(,) or Integer()() (C# int[,] and int[][]).

Bill McCarthy suggestion for one-dimensional collections seems to be popular:

Dim x = {1, 2, 3, 4} 'List(Of Int32)
Dim x() = {1, 2, 3, 4} 'array of Int32

And to handle dictionaries, Ninputer offers this approach:

Dim d = {1:"Hello", 2:"World" }

You can follow the debate on Paul Vick's blog.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT