为什么我要把我的物品放在袋子里?

发布于 2024-10-24 23:21:44 字数 420 浏览 10 评论 0原文

我刚刚看到一个关于 System.Collections.ConcurrentBag类的问题,并且我看到了 ControllerViewBag 属性在 ASP.NET MVC 中。根据我的经验,我了解到,如果您了解人们编写代码的确切目的,那么使用他们的代码会更容易。我认为它对于 ListDictionaryReadOnlyCollection 的含义非常直观代表。另一方面,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人生百味 2024-10-31 23:21:44

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:

  • 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.

撩起发的微风 2024-10-31 23:21:44

来自 MSDN 文档的第一行:

表示线程安全、无序的对象集合。

我认为这并不比“List”或“ReadOnlyCollection”更直观,真的 - YMMV。

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.

独﹏钓一江月 2024-10-31 23:21:44

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文