Collection Initializers

Posted Thu, Apr 29 2010 15:08 by Deborah Kurata

One of the new features in VB 10.0 is collection initializers. Collection initializers allow you to initialize an array or list in a single line of code.

(C# has had collection initializers since C# 3.0. The examples here show both C# and VB for completeness.)

This example uses the customer class defined here and builds a list of customers using collection initializers.

In VB:

Dim custList As New List(Of Customer) From
            {New Customer() With
                  {.CustomerId = 1,
                    .FirstName = "Bilbo",
                    .LastName = "Baggins",
                    .EmailAddress = "bb@hob.me"},
            New Customer() With
                  {.CustomerId = 2,
                    .FirstName = "Frodo",
                    .LastName = "Baggins",
                    .EmailAddress = "fb@hob.me"},
            New Customer() With
                  {.CustomerId = 3,
                    .FirstName = "Samwise",
                    .LastName = "Gamgee",
                    .EmailAddress = "sg@hob.me"},
            New Customer() With
                  {.CustomerId = 4,
                    .FirstName = "Rosie",
                    .LastName = "Cotton",
                    .EmailAddress = "rc@hob.me"}}

In C#:

List<Customer> custList = new List<Customer>
        {new Customer()
              { CustomerId = 1,
                FirstName="Bilbo",
                LastName = "Baggins",
                EmailAddress = "bb@hob.me"},
        new Customer()
              { CustomerId = 2,
                FirstName="Frodo",
                LastName = "Baggins",
                EmailAddress = "fb@hob.me"},
        new Customer()
              { CustomerId = 3,
                FirstName="Samwise",
                LastName = "Gamgee",
                EmailAddress = "sg@hob.me"},
        new Customer()
              { CustomerId = 4,
                FirstName="Rosie",
                LastName = "Cotton",
                EmailAddress = "
rc@hob.me"}};

Notice how these two code snippets are very similar. VB even has the same curly braces!

The key differences are that VB requires the From keyword when initializing the list, the With keyword when initializing each customer in the list, and the "." in front of the property names.

Compare this VB example to the one shown for VB 9 (VS 2008) here.

Use this technique whenever you need to initialize a list or array of objects.

Enjoy!

Filed under: , , , , ,

Comments

# Lambda Expressions: Zip

Tuesday, May 04, 2010 11:33 PM by Deborah's Developer MindScape

One of the new features in C# 4 and VB 10 (.NET 4.0) is the Zip extension method on IEnumerable. This

# Bay.NET Event: .NET 4.0 and VS 2010

Friday, May 14, 2010 7:52 PM by Deborah's Developer MindScape

We are lucky to have two of Redmond's finest down here in Silicon Valley for a full day .NET 4.0

# Bay.NET Event: .NET 4.0 and VS 2010

Friday, May 14, 2010 7:53 PM by Deborah's Developer MindScape

We are lucky to have two of Redmond's finest down here in Silicon Valley for a full day .NET 4.0

# re: Collection Initializers

Wednesday, May 26, 2010 10:21 PM by Stan Schultes

Deborah -

Note that you can dramatically shorten the data initialization syntax in VB to this:

       Dim list As New List(Of Customer) From

           {

               {1, "Bilbo", "Baggins", "bb@hob.me"},

               {2, "Frodo", "Baggins", "fb@hob.me"},

               {3, "Samwise", "Gamgee", "sg@hob.me"},

               {4, "Rosie", "Cotton", "rc@hob.me"}

           }

With the addition of an Add extension method for List(Of Customer). Be sure to place the extension method in a Module:

   <Extension()>

   Sub Add(ByVal list As List(Of Customer),

           ByVal CustomerID As Integer,

           ByVal FirstName As String,

           ByVal LastName As String,

           ByVal EmailAddress As String)

       list.Add(New Customer With

                {

                   .CustomerId = CustomerID,

                   .FirstName = FirstName,

                   .LastName = LastName,

                   .EmailAddress = EmailAddress

                })

   End Sub

Thanks for sharing!

Stan

# re: Collection Initializers

Thursday, May 27, 2010 10:21 AM by Deborah Kurata

Hi Stan -

Thanks for your post!

# Building a List of of Items with Child Items

Saturday, October 30, 2010 7:47 PM by Deborah's Developer MindScape

In this prior post , I demonstrated how to build a generic list of items. The example built a list of

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: