.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

Does VB.NET have Collection Initializers?

The short answer is no, at least not in the same sense as C#. Here is a snippet on how to initialize a collection in C#.


  List<Phone> oPhone = new List<Phone> {

       new Phone { PersonId = 1, Number = "111.111.1111" },

       new Phone { PersonId = 2, Number = "222.222.2222" }

  };


As you can see , in C#, it is possible to initialize a collection in one line of code. To my knowledge, in VB.NET, it requires an additional line of code and the use of a built-in extension method.


  Dim arrPhone As Phone() = { _

       New Phone With {.PersonId = 1, .Number = "111.111.1111"}, _

       New Phone With {.PersonId = 2, .Number = "222.222.2222"} _

  }

  Dim phoneList As List(Of Phone) = arrPhone.ToList()

Guess the movie

Hey, listen, I want somebody good - and I mean very good - to plant that gun. I don't want my brother coming out of that toilet with just his dick in his hands, alright?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: VB.NET
Posted by CynotWhyNot on Monday, March 17, 2008 12:28 PM
Permalink | Comments (72) | Post RSSRSS comment feed