c# - Array.Clear 实际上做了什么?

标签 c# .net exception clr cil

我正在寻找有关 Array.Clear(...) 方法在 C# 中的作用的答案。

我查看了 IL,但这并没有真正产生任何线索,因为它只是调用 mscorlib 中的 System.Array::Clear(...) 方法,然后该方法调用我无法观察到的 CLR 的非托管部分。

我问这个问题的原因是,我偶尔会在调用 Array.Clear 时抛出 SEHException,而且我似乎无法弄清楚为什么会这样。

不幸的是,微软似乎对抛出异常时可能意味着什么守口如瓶......

发件人:http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.sehexception(v=VS.100).aspx

Any SEH exception that is not automatically mapped to a specific exception is mapped to the SEHException class by default. For more information, search on "unmanaged exceptions" and "Structured Exception Handling" in the MSDN Library.

最佳答案

最容易设想 Array.Clear 是这样写的

public static void Array.Clear<T>(T[] array) {
  for (int i = 0; i < array.Length; i++) {
    array[i] = default(T);
  }
}

我意识到 Array.Clear 实际上并不是一种通用方法,我正在尝试演示对引擎盖下发生的事情的紧密映射。真的虽然更接近

memcopy(&array, 0, array.Length * sizeof(T));

如果此代码抛出 SEHException,则最可能的原因是源数组周围的内存已损坏。最可能的来源是不正确的 PInvoke 或 COM 互操作调用。

关于c# - Array.Clear 实际上做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5557609/

相关文章:

c# - IEnumerable 模型中的多个 DropDownList

c# - 鼠标悬停文本框时删除浅蓝色边框

c# - WinForms WebBrowser 中的拼写检查

c# - 在 C# 中更改上下文菜单字体大小

java - "java.net.NoRouteToHostException: Cannot assign requested address"是什么?

NHibernate 映射异常 : Could not compile the mapping document

c# - 在 C# 中绘图的代码效率?

c# - 光标位置函数返回错误值

c# - 从电子邮件列表中获取唯一域的列表

c++ - Linux 上共享对象 (.so) 的异常