Click here to Skip to main content
14,450,291 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
GeneralRe: Unions in C# Pin
Eytukan10-Sep-18 4:53
MemberEytukan10-Sep-18 4:53 
GeneralRe: Unions in C# Pin
Chris Maunder10-Sep-18 20:59
cofounderChris Maunder10-Sep-18 20:59 
GeneralRe: Unions in C# Pin
Michael Martin10-Sep-18 22:20
professionalMichael Martin10-Sep-18 22:20 
GeneralRe: Unions in C# Pin
Chris Maunder10-Sep-18 22:44
cofounderChris Maunder10-Sep-18 22:44 
GeneralRe: Unions in C# Pin
Member 798912210-Sep-18 22:42
MemberMember 798912210-Sep-18 22:42 
GeneralRe: Unions in C# Pin
Chris Maunder10-Sep-18 22:46
cofounderChris Maunder10-Sep-18 22:46 
GeneralRe: Unions in C# Pin
Maximilien10-Sep-18 5:44
MemberMaximilien10-Sep-18 5:44 
GeneralRe: Unions in C# PinPopular
Member 798912210-Sep-18 6:46
MemberMember 798912210-Sep-18 6:46 
Vunic wrote:
oops just seeing it's not there natively. (Not sure why)
Union seems to me more or less meaningless in managed code. At least it it contrary to the philosophy of managed code: You don't have a "memory address" but a handle. The runtime system will interpret that handle, translating it to "some" memory address that you shouldn't care about at all.

The runtime system has the right to to that handle-to-address mapping in whatever way it pleases: It can do a JIT compilation (for code handles) and put the result into the assembly cache, it can load the data object from some backing store (in principle even magnetic tape!) etc.

A union says: Let these two logical objects (usually fields in a class/struct), the two handles, be translated to the same physical address. That would strongly restrict the freedom of the mapping operation of the runtime system. What if the alternative interpretations have different sizes? What if one alternative contains a partial object, addressed through a handle, while another is a flat array? What if one member is an accessor function? Or a delegate? What about a list object unionized with a string? If the semantics of overlaying wasn't unambiguously defined, it would be useless, so you would have to define all the details of cases like these mentiones, and the runtime system would have to imlement exactly that (rather hairy) semantics.

I am really happy that we have kicked unions out the back door.

If you really need that functionality, then you declare a byte array, to hold the "memory block", and define accessors that select parts of that byte array, and by shifting and masking and explicit casting provides other interpretations of the "memory block". It is not done behind the curtain, where the reader would have to know how the compiler and runtime system works on the bit level. Rather, you code it all yourself, explicitly and with a big warnig sign: Danger! Arbitrary, unchecked casts made here! Then you define the overlaying in every detail, completely independent of how the compiler and runtime system treats your data.

Yes, it will cost you a lot of work. It should. To keep you away from doing it.
GeneralRe: Unions in C# Pin
Eytukan10-Sep-18 7:59
MemberEytukan10-Sep-18 7:59 
GeneralRe: Unions in C# Pin
jeron110-Sep-18 8:14
Memberjeron110-Sep-18 8:14 
GeneralRe: Unions in C# Pin
Eytukan10-Sep-18 8:46
MemberEytukan10-Sep-18 8:46 
GeneralRe: Unions in C# Pin
Sander Rossel10-Sep-18 10:26
professionalSander Rossel10-Sep-18 10:26 
GeneralRe: Unions in C# Pin
Eytukan12-Sep-18 20:28
MemberEytukan12-Sep-18 20:28 
GeneralRe: Unions in C# Pin
CPallini10-Sep-18 12:04
mveCPallini10-Sep-18 12:04 
GeneralRe: Unions in C# Pin
Eytukan12-Sep-18 20:26
MemberEytukan12-Sep-18 20:26 
GeneralRe: Unions in C# Pin
CPallini13-Sep-18 0:54
mveCPallini13-Sep-18 0:54 
GeneralRe: Unions in C# Pin
englebart11-Sep-18 3:07
professionalenglebart11-Sep-18 3:07 
GeneralRe: Unions in C# Pin
DSewhuk11-Sep-18 9:21
MemberDSewhuk11-Sep-18 9:21 
GeneralRe: Unions in C# Pin
Jerry W. Manweiler, Ph.D.11-Sep-18 12:31
MemberJerry W. Manweiler, Ph.D.11-Sep-18 12:31 
GeneralRe: Unions in C# Pin
Eytukan12-Sep-18 20:26
MemberEytukan12-Sep-18 20:26 
GeneralData Dictionaries Pin
NeverJustHere10-Sep-18 0:42
MemberNeverJustHere10-Sep-18 0:42 
GeneralRe: Data Dictionaries Pin
lw@zi 10-Sep-18 1:19
professional lw@zi 10-Sep-18 1:19 
GeneralRe: Data Dictionaries Pin
#realJSOP10-Sep-18 3:30
mva#realJSOP10-Sep-18 3:30 
GeneralRe: Data Dictionaries Pin
Mycroft Holmes10-Sep-18 15:35
MemberMycroft Holmes10-Sep-18 15:35 
GeneralRe: Data Dictionaries Pin
englebart11-Sep-18 3:15
professionalenglebart11-Sep-18 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.