문제

I have a struct:

struct S {
    public readonly int Value1;
    public readonly int Value2;
    public S(int value1, int value2) {
        this.Value1 = value1;
        this.Value2 = value2;
    }
}

and I try to take the address of Value2:

var s = default(S);
unsafe {
    var r = new IntPtr(&s.Value2);
}

but I get a compiler error:

Cannot take the address of the given expression

I thought I could take the addresses of fields? What's going on?

도움이 되었습니까?

해결책

Apparently it doesn't work with readonly fields. Changing S to this:

struct S {
    public int Value1;
    public int Value2;
}

fixes the problem.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top