| title | How to: Create a Collection Used by a Collection Initializer (Visual Basic) | |
|---|---|---|
| ms.custom | ||
| ms.date | 07/20/2015 | |
| ms.prod | .net | |
| ms.reviewer | ||
| ms.suite | ||
| ms.technology |
|
|
| ms.topic | article | |
| helpviewer_keywords |
|
|
| ms.assetid | c858db10-424d-47e0-92cd-e08087cc5ebc | |
| caps.latest.revision | 8 | |
| author | dotnet-bot | |
| ms.author | dotnetcontent |
When you use a collection initializer to create a collection, the Visual Basic compiler searches for an Add method of the collection type for which the parameters for the Add method match the types of the values in the collection initializer. This Add method is used to populate the collection with the values from the collection initializer.
The following example shows an OrderCollection collection that contains a public Add method that a collection initializer can use to add objects of type Order. The Add method enables you to use the shortened collection initializer syntax.
[!code-vbVbVbalrCollectionInitializersHowTo2#4]
[!code-vbVbVbalrCollectionInitializersHowTo2#1]
[!code-vbVbVbalrCollectionInitializersHowTo2#2]
[!code-vbVbVbalrCollectionInitializersHowTo2#3]
Collection Initializers
How to: Create an Add Extension Method Used by a Collection Initializer