如果将项目两次添加到集合中会发生什么?
如果您尝试将某个项目添加到集合中已存在的集合中,会发生什么情况?
假设您有一个充满整数的集合,并且您尝试添加集合中已有的整数。
What happens if you try to add an item to a Collection that already exists in the Collection?
Let's say you have a Collection full of integers and you try to add an integer that is already in the Collection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单地说,如果您将某个项目添加到集合中两次,则该项目将在集合中出现两次。
如果您使用密钥添加它,那么它必须是唯一的。
Simply put if you add an item to a collection twice, it will be in the collection twice.
If you add it with a key then that needs to be unique.
如果您尝试添加与集合中已有的键相同的键,则会引发错误 (文档)。只要使用不同的键,您就可以将同一个对象添加到集合中两次。
旁注:微软结束了近四年来对 VB6 开发环境的所有官方支持前。至少通过 Windows 7 仍然支持运行时(例如,以便现有应用程序继续运行)。有关更多详细信息,请参阅另一个问题及其答案。
If you try to add something with the same key as a key already in the collection, it throws an error (docs). You can add the same object to a collection twice as long as you use different keys for it.
Side note: Microsoft ended all official support for the VB6 development environment nearly four years ago. The runtime remains supported at least through Windows 7 (e.g., so existing apps continue to run). See this other question and its answers for more details.