Pregunta

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?

¿Fue útil?

Solución

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

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

fixes the problem.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top