c# – 获取ref参数的地址
发布时间:2020-12-15 21:21:28 所属栏目:百科 来源:网络整理
导读:我的代码如下 class MyClass{ static int iField = 42; static void Test(ref int arg) { unsafe { fixed(void* pField = iField) fixed(void* pArg = arg) { Console.WriteLine ("{0},{1}",(int)pArg,(int)pField); //output: 165451772,165451772 } } } st
我的代码如下
class MyClass { static int iField = 42; static void Test(ref int arg) { unsafe { fixed(void* pField = &iField) fixed(void* pArg = &arg) { Console.WriteLine ("{0},{1}",(int)pArg,(int)pField); //output: 165451772,165451772 } } } static void Main() { Test(ref iField); } } 我想知道参考ref参数的地址是否与上面测试中显示的一样可靠. 解决方法
据我所知,是的,当你在固定区块时,GC不会重新定位arg.一旦在固定块之外,变量就被取消固定,因此需要由GC重新定位.
关于一个静态结构/类/字段保持其地址固定在内存中,答案是否定的.它们像任何其他对象一样被重新定位.由于它被标记为静态,因此没有保证在内存中不会有未来的重定位. 静态的意义与编译器在某个类/结构/字段的编译时具有的知识和确定性有关.它与内存位置无关,它们是否固定等等.用Eric Lippert的话来说:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |