2

I just saw an SO question about the System.Collections.ConcurrentBag<T> class, and I've seen the ViewBag property of the Controller in ASP.NET MVC. In my experience, I've learned that it's easier to use people's code if you understand what exactly they were getting at in writing it. I think its pretty intuitive as to what a List<T> or a Dictionary<TKey,TValue> or a ReadOnlyCollection<T> are meant to represent. A Bag on the other hand is not so intuitive.

So, my question is: What is this Bag metaphor meant to represent, specifically with respect to the .NET framework?

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • Why is `List` intuitive? When I (and others with FP backgrounds) think of a list, I may think of a head and a tail. Something more akin to a linked list. Something without random access. – R. Martinho Fernandes Mar 21 '11 at 12:27
  • @Martinho, look at my comment on Dan's answer – smartcaveman Mar 21 '11 at 12:35
  • 2
    The word 'bag' causes lots of confusion. They should have called it ConcurrentCache but couldn't, the word "cache" is already overloaded too much. – Hans Passant Mar 21 '11 at 14:18
  • Why couldn't they just use the world collection? System.Collections.ConcurrentCollection – Nick Sep 22 '12 at 03:32
  • @Nick, A bag is necessarily a collection. A collection is not necessarily a bag. A bag has additional invariants (see Skeet's answer). – smartcaveman Sep 24 '12 at 16:20
  • I did see Skeet's answer. He said that a bag is: unordered, can contain duplicates, is just values rather than a map. All of those things are a collection. The only difference is the thread-safe part, but that's why i said why not `ConcurrentCollection` and did not say just `Collection`. What else is different between a "Bag" and a "Collection"? – Nick Sep 24 '12 at 18:16
  • @Nick, a Collection is not necessarily unordered. It is order-agnostic. For example, a List is a collection that is necessarily ordered. A Bag is necessarily unordered. The consequence is that an object can be both a Collection and a Bag or both a Collection and a List, but an object cannot be both a Bag or a List, because their invariant conditions conflict. So, basically you're statement that a Collection is unordered is false. A Collection may or may not be ordered. – smartcaveman Sep 24 '12 at 20:37
  • @Nick, If you still have trouble understanding the distinction it may be helpful for you to read about the [Liskov substitution principle](http://en.wikipedia.org/wiki/Liskov_substitution_principle) – smartcaveman Sep 24 '12 at 20:38
  • If I was talking about a List I would have said List – Nick Sep 25 '12 at 00:56

3 Answers3

6

ConcurrentBag<T> is a thread-safe, unordered sequence of items which can include duplicates.

So compared with some other collections:

  • It's unordered, like a HashSet<T> but unlike a List<T>
  • It can contain duplicates, like a List<T> but unlike a HashSet<T>
  • It's just values rather than a map (unlike Dictionary<TKey, TValue>)
  • It's thread-safe, unlike all the non-concurrent collections - so you can share an instance between multiple threads, all reading and writing.

Possible uses include a work queue where you don't care about the ordering (as otherwise you'd use ConcurrentQueue / ConcurrentStack) or a list of items where you'll always apply a sort order after fetching the data into another "local" collection.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Does it work faster than a `List` as a result of being unordered? – smartcaveman Mar 21 '11 at 12:26
  • @smartcaveman: The point is that it's thread-safe - so you don't need to take out all the locks which would be needed to safely share a `List` between multiple threads. I'll update my answer... – Jon Skeet Mar 21 '11 at 12:33
  • @Jon, While helpful and necessary for a complete answer, unless the update you added is applicable to the `ViewBag` as well, then I think your answer could potentially be misleading unless you qualify that you are referring specifically to `ConcurrentBag` – smartcaveman Mar 21 '11 at 12:37
  • @smartcaveman: Updated, thanks. I haven't come across ViewBag myself, so can't comment on it. – Jon Skeet Mar 21 '11 at 12:40
  • 1
    @Jon, the concept of you not knowing something about .NET just shattered my reality – smartcaveman Mar 21 '11 at 12:45
  • @smartcaveman - `ViewBag` is simply a *property name* on a class. As amazing as Jon is, you can't expect him to know *every property on every class* in .NET. In the context of a controller, I it's just a *name* that was chosen. It essentially represents a collection of key/value pairs (in MVC3 it's dynamic - but the same idea applies). See Hans comment on the OP. – TheCloudlessSky Mar 21 '11 at 15:11
0

From the first line of the MSDN documentation:

Represents a thread-safe, unordered collection of objects.

I don't think that's any less intuitive than "List" or "ReadOnlyCollection", really - YMMV.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • I guess it's a matter of opinion, but you really don't think its easier to visualize what a list is and what you can do with a list than what a bag is and what you can do with a bag? I think the concept of a list is much more finite in its semantic definition than a bag is. I mean, check out the variance in definitions on http://dictionary.reference.com/browse/list and http://dictionary.reference.com/browse/bag . It just seems like you need less of a technology background to understand what a `List` is than a `Bag`... But, it's still just opinion – smartcaveman Mar 21 '11 at 12:32
  • @smartcaveman: it's definitely a matter of opinion and background. But in a data structures context, "bag" is a more precise term than "list" is. People use "list" to refer to many different (but similar) data structures (an array? a linked list? an FP list?), but when you mention a "bag" there's no doubt about which data structure you're talking about (unless the other person never heard of it, of course). – R. Martinho Fernandes Mar 21 '11 at 12:40
  • @Martinho, agreed. I try to keep my programming metaphors as close to a "real world" context as possible in order to keep from getting confused. This is what I meant by intuitive. However, in the context from which you are approaching it, I completely see where you and Dan are coming from. – smartcaveman Mar 21 '11 at 12:50
  • 2
    @smartcaveman: if you want a metaphor for a bag, well, think of a bag. As in, those plastic containers used for groceries. You can put all kinds of stuff in it, but you can't keep them ordered. And there's nothing to prevent you from putting two apples in there. Hope this helps :) – R. Martinho Fernandes Mar 21 '11 at 12:58
0

ConcurrentBag is the same as ConcurrentStack and ConcurrentQueue except that it doesn't maintain ordering for performance reasons, this article explain it in more details http://www.emadomara.com/2011/08/what-is-concurrentbag.html

Emad Omara
  • 512
  • 4
  • 3