Real UseCase for using FieldOffSet considering the example
-
Hi, Reading CLR via C# on page 124 there is discussion about altering Layout kind for value types for InterOperability and there is example quoted which uses same
FieldOffSet
for multiple fields. I understand the the memory of both fields overlap and modifying one field will cause value of other field to be changed but what i am thinking about is the real use case where we would need it or have been used in past? here is the code for example:[StructLayout(LayoutKind.Explicit)]
public struct SomeValType {
[FieldOffset(0)]
public Int16 m_b; // The m_b and m_x fields overlap each
[FieldOffset(0)]
public Int32 m_x; // other in instances of this type
}
}and when i wrote the following code in Main it caused to have 10 as value in the
m_x
field :SomeValType obj = new SomeValType();
obj.m_b = 10;
Console.WriteLine(obj.m_x);Can someone please shed some light regarding this. it will be highly appreciated. Thanks!
-
Hi, Reading CLR via C# on page 124 there is discussion about altering Layout kind for value types for InterOperability and there is example quoted which uses same
FieldOffSet
for multiple fields. I understand the the memory of both fields overlap and modifying one field will cause value of other field to be changed but what i am thinking about is the real use case where we would need it or have been used in past? here is the code for example:[StructLayout(LayoutKind.Explicit)]
public struct SomeValType {
[FieldOffset(0)]
public Int16 m_b; // The m_b and m_x fields overlap each
[FieldOffset(0)]
public Int32 m_x; // other in instances of this type
}
}and when i wrote the following code in Main it caused to have 10 as value in the
m_x
field :SomeValType obj = new SomeValType();
obj.m_b = 10;
Console.WriteLine(obj.m_x);Can someone please shed some light regarding this. it will be highly appreciated. Thanks!
Don't post the same question in multiple places - it just duplicates work and annoys people: Real usecase for same fieldoffset on multiple fields[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Don't post the same question in multiple places - it just duplicates work and annoys people: Real usecase for same fieldoffset on multiple fields[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
how to delete this one, as someone answered other place mine question, the delete button is disabled.
-
how to delete this one, as someone answered other place mine question, the delete button is disabled.
You can't, because there is a reply. But...I can, so consider it done...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!