anonymous types and generic collections

Posted Sun, Apr 8 2007 19:54 by bill
Tim Ng has made a post about using a helper function to create a generic list for anonymous types.
The code actually needs a bit of modification, it should really be :
 
Function MakeList(Of T)(ByVal ParamArray items() As T) As List(Of T)
   Dim list As New List(Of T)
   list.AddRange(items)
   Return list
End Function
 
You can then pass to it any number of items for your anonymous type.
 
Still, that being the case, the inference could be made at a more direct level if the language supported an anonymous type placeholder, be it var, ? or some other symbol. That is, it should be possible to write code such as :
 
Dim things As ? = New List(Of ?) {New With {.Description= "meaning of life", .Age = 42}, _
                                             New With {.Description= "meaning of life sequel", .Age = 43}}
 
And hence remove the need for yet another helper function.
 
hopefully we can elegantly make the language evolve in such a direction :)       
 
 
 
 
Filed under: , ,