I am able to get the address of a struct outside its (instance) method, as shown below:
public struct Values
{
public byte A;
public byte B;
public byte C;
public byte D;
public static unsafe bool Equals(Values lhs, Values rhs)
{
return *(int*) &lhs == *(int*) &rhs;
}
}
But when I try to get the address of the struct itself, the IDE tells me it's wrong:
public unsafe bool Equals(Values other)
{
return *(int*) &this == *(int*) &other;
}
The error message is:
You can only take the address of an unfixed expression inside of a fixed statement initializer.
The fixed statement prevents the garbage collector from relocating a movable variable. But this struct is not a movable variable, and will not be garbage collected, right?
Updated
What I actually need is to get/set the value of the Nth bytes by index. Although I can do it through switch statement, but it will be faster through index. Thanks to @KonradKokosa, Fixed Size Buffer meets my needs. I still want to know the answer to the main question.