- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码不会抛出,即使在枚举过程中修改了枚举集合。
using System;
using System.Collections;
using System.Runtime.CompilerServices;
namespace ConsoleApp1
{
class MyKey
{
}
class Program
{
static void Main(string[] args)
{
ConditionalWeakTable<MyKey, string> table = new();
MyKey k1 = new();
MyKey k2 = new();
MyKey k3 = new();
table.Add(k1, "v1");
table.Add(k2, "v2");
table.Add(k3, "v3");
var enumerator = ((IEnumerable)table).GetEnumerator();
while(enumerator.MoveNext()) // no exception thrown
{
Console.WriteLine(enumerator.Current);
table.Remove(k1);
table.Remove(k2);
table.Remove(k3);
}
}
}
}
[ConsoleApp1.MyKey, v1]
这是故意的还是偶然的?如果是前者,那么在 key 被垃圾收集的情况下,什么会阻止抛出异常?
非常感谢!
最佳答案
按设计。
引自 the docs .
The returned enumerator does not extend the lifetime of any object pairs in the table, other than the current one. It does not return entries that have already been collected or that were added after the enumerator was retrieved. Additionally, it may not return all entries that were present when the enumerator was retrieved, for example, entries that were collected or removed after the enumerator was retrieved but before they were enumerated.
枚举器的 Current
属性指向的对象不会被垃圾回收,可以安全访问。如果可枚举中更远的键被 GC 处理或删除,枚举器将不会访问该元素。
正如 canton7 在评论中提到的,这是 ConditionalWeakTable
保持线程安全所必需的 - 其他线程可能会在另一个线程的枚举期间删除元素。
关于c# - 为什么从 ConditionalWeakTable 中删除键时 IEnumerator.MoveNext 不抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70151906/
.NET 应用程序崩溃并显示堆栈跟踪: 调用堆栈: Layouts!Layouts.Ribbon.SizeAndPositionControlViewModel+OnLayoutSelectionCh
我有一个简单的代码如下: foreach ($someFile in Get-ChildItem) { if (($someFile.Name.Substring(0,1) -eq "R")
IEnumerator.MoveNext() 的实现是否预计会相对较快?或者如果“移动到下一项” 包括磁盘 IO、Web 请求或其他可能长时间运行的操作是否可以? 例如,我正在处理一个处理文档的项目,
我是 c# 的新手,我知道必须实现接口(interface)中定义的方法 但在下面的代码中我没有实现 MoveNext() 方法 static void Main() { List list
在集合上使用 Linq,哪个最适合发现集合不为空? HasChild = Childs.GetEnumerator().MoveNext() ? true : false; 和 HasChild =
使用 log4net 声明为: private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod(
有人可以解释为什么这段代码在无限循环中运行吗?为什么 MoveNext() 总是返回 true? var x = new { TempList = new List { 1, 3, 6, 9 }.Ge
我正在努力实现 100% 的代码覆盖率。然而,代码覆盖以某种方式提示某些未涵盖的 MoveNext() 方法,但是,没有迭代 IEnumerable 的代码路径... 关于如何覆盖 MoveNext
我正在努力实现 100% 的代码覆盖率。然而,代码覆盖以某种方式提示某些未涵盖的 MoveNext() 方法,但是,没有迭代 IEnumerable 的代码路径... 关于如何覆盖 MoveNext
我有一个 WebAPI它具有以下代码段,其中在使用我的记录器记录的 try 块中编写的代码中存在 Null 引用异常。 但是在TargetSite的Exception登录,我收到 无效 MoveNex
我是 IL 的新手,但据我所知,MoveNext 应该在 Current 之前调用,假设我们有这样的 foreach 语句: foreach (var i in Enumerable.Empty())
考虑以下代码: List list = new List(); IEnumerable enumerable = list; IEnumerator enumerator = enumerable.G
我正在使用 crm Dynamics 365 许可 我有 BPF(业务流程) 我想使用 javascript 前进到最后一个阶段,但它仅使用此代码移动一个阶段: for (var i = 0; i <
我试图在下面的示例中获取异常堆栈跟踪的最内部方法名称。由于未知原因,我总是得到 MoveNext 方法名称,而不是真实名称。我该如何解决? var st = new StackTrace(ex, tr
构建二叉搜索树后BST其中包括 BSTNode我正在尝试为其实现 IEnumerable 接口(interface)。 这就是我构建 BSTNodeEnumrator 的方式: public clas
我正在使用 MongoDB 实现 PubSub。如果我创建查询并指定带有 NoCursorTimeout 的 TailableCursor: using(var enumerator = _Colle
下面的代码不会抛出,即使在枚举过程中修改了枚举集合。 using System; using System.Collections; using System.Runtime.CompilerServ
你好, 我在使用一些简单的 VBA 时遇到了问题。我编写了一个脚本来进行文本查询(没有输入参数,因此没有真正的用户交互、SQL 注入(inject)等),针对数据库运行它,并将其转储到新的工作表中。这
This thread表示 LINQ 的 OrderBy 使用快速排序。考虑到 OrderBy 返回一个 IEnumerable,我正在努力理解它的意义。 我们以下面这段代码为例。 int[] arr
我正试图找出我们代码中的错误。我把它归结为下面的片段。在下面的示例中,我有一个 int 网格(行列表),但我想找到具有 1 的列的索引。其实现是为每一行创建一个枚举器,并逐步遍历每一列通过保持普查员步
我是一名优秀的程序员,十分优秀!