ConcurrentBag(Of T)

The ConcurrentBag(Of T) is the most basic concurrent collection, in that it is just an unordered collection of items. The following code demonstrates how you use it for adding, iterating, counting, and removing items:

'Creating an instanceDim cb As New ConcurrentBag(Of String)'Adding some itemscb.Add("String one")cb.Add("String two")cb.Add("String three")'Showing items countConsole.WriteLine(cb.Count)'Listing items in the collectionFor Each item In cb    Console.WriteLine(item)Next'Removing an itemDim anItem As String = String.Emptycb.TryTake(anItem)Console.WriteLine(anItem)

You add items to the collection by invoking the Add method. The Count property gets the number of items in the collection, ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.