创建指向自定义类的指针 c#

问题描述 投票:0回答:0

所以我有任何自定义类测试:

class Test
{
    public int value;
    public Test(int _value)
    {
        this.value = _value;
    }
    
    public unsafe Test(string _value)
    {
        foreach (char chr in _value)
        {
            unchecked
            {
                this.value += (int)&chr; //does stuff
            }
            
        }
    }
}

为什么我不能在main函数中写:

Test t = new Test(9);
long* pTest = (long*)&t;
Console.WriteLine(pTest); //should give a address location

报错:

不能获取托管类型“测试”变量的地址

有什么办法可以让它发挥作用吗?如果 this.value 具有另一个自定义类的类型怎么办?请帮助。

c# pointers memory-management
© www.soinside.com 2019 - 2024. All rights reserved.