Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, Reading book CLR via C# on page 124 there is discussion about altering Layout kind for value types for Inter-Operability and there is example quoted which uses same FieldOffSet for multiple fields.

What I have tried:

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:


C#
[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!
Posted
Updated 20-Jul-17 3:54am

1 solution

Hmmm, interesting question, but this takes a lot of things back to the old schools and might require some low-level stuff to be discussed, which I personally ignore when it comes to C#.

First thing to realize is, that you only want to rearrange the bits and their locations if you know exactly what you're doing, and why you are doing. When I read your question, the first thing that came to my mind after reading,
Quote:
modifying one field will cause value of other field to be changed
was the use of union in C, where your type had the maximum size set to the largest field and could've hold one value. Same case would be here, if you have 5 fields (all with the FieldOffset(0)) then they would all modify previous content. Please read a bit more about unions and struct difference here on the following links (I know you are aware of these, but still for the sake of future visitors),

Unions in C[^]
struct - Difference between a Structure and a Union in C - Stack Overflow[^]

Excerpt from the last link,
Quote:
With a union, you're only supposed to use one of the elements, because they're all stored at the same spot.

I was also thinking about references, where you modify the previous stuff but that is something else and doesn't modify fields of the same object, instead work directly on the objects.

Finally, you need to check the size of the variables as well, one of your variables was Int16, and other was Int32, they have different sizes so thus they are definitely going to cause a problem or maybe faults in the low-level memory — Which I am unsure of, thus won't go in-depth of. But the thing is, never use it in real. :laugh:

In C#, or even C++ I do not see any benefit of having this, where you are going to store only one field at a time and rest of the fields are either garbage or not used at all, why not have a variable in the first place?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900