为什么我要把我的物品放在袋子里?
我刚刚看到一个关于 System.Collections.ConcurrentBagController
的 ViewBag
属性在 ASP.NET MVC 中。根据我的经验,我了解到,如果您了解人们编写代码的确切目的,那么使用他们的代码会更容易。我认为它对于 List
或 Dictionary
或 ReadOnlyCollection
的含义非常直观代表。另一方面,Bag
就不那么直观了。
所以,我的问题是:这个 Bag
隐喻代表什么,特别是相对于 .NET 框架?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ConcurrentBag
是线程安全的、无序的项目序列,可以包含重复项。因此,与其他一些集合相比:
HashSet
但与List
不同List;
但与HashSet
不同,Dictionary)
可能的用途包括您不关心顺序的工作队列(否则您将使用
ConcurrentQueue
/ConcurrentStack
)或您始终需要的项目列表将数据提取到另一个“本地”集合后应用排序顺序。ConcurrentBag<T>
is a thread-safe, unordered sequence of items which can include duplicates.So compared with some other collections:
HashSet<T>
but unlike aList<T>
List<T>
but unlike aHashSet<T>
Dictionary<TKey, TValue>
)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.来自 MSDN 文档的第一行:
我认为这并不比“List”或“ReadOnlyCollection”更直观,真的 - YMMV。
From the first line of the MSDN documentation:
I don't think that's any less intuitive than "List" or "ReadOnlyCollection", really - YMMV.
ConcurrentBag 与 ConcurrentStack 和 ConcurrentQueue 相同,只是出于性能原因它不维护顺序,本文更详细地解释它 http://www.emadomara.com/2011/08/what-is-concurrentbag.html
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