Click here to Skip to main content
15,897,371 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 3:53
Eytukan10-Sep-18 3:53 
GeneralRe: Unions in C# Pin
Chris Maunder10-Sep-18 19:59
cofounderChris Maunder10-Sep-18 19:59 
GeneralRe: Unions in C# Pin
Michael Martin10-Sep-18 21:20
professionalMichael Martin10-Sep-18 21:20 
GeneralRe: Unions in C# Pin
Chris Maunder10-Sep-18 21:44
cofounderChris Maunder10-Sep-18 21:44 
GeneralRe: Unions in C# Pin
kalberts10-Sep-18 21:42
kalberts10-Sep-18 21:42 
GeneralRe: Unions in C# Pin
Chris Maunder10-Sep-18 21:46
cofounderChris Maunder10-Sep-18 21:46 
GeneralRe: Unions in C# Pin
Maximilien10-Sep-18 4:44
Maximilien10-Sep-18 4:44 
GeneralRe: Unions in C# PinPopular
kalberts10-Sep-18 5:46
kalberts10-Sep-18 5: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 6:59
Eytukan10-Sep-18 6:59 
GeneralRe: Unions in C# Pin
jeron110-Sep-18 7:14
jeron110-Sep-18 7:14 
GeneralRe: Unions in C# Pin
Eytukan10-Sep-18 7:46
Eytukan10-Sep-18 7:46 
GeneralRe: Unions in C# Pin
Sander Rossel10-Sep-18 9:26
professionalSander Rossel10-Sep-18 9:26 
GeneralRe: Unions in C# Pin
Eytukan12-Sep-18 19:28
Eytukan12-Sep-18 19:28 
GeneralRe: Unions in C# Pin
CPallini10-Sep-18 11:04
mveCPallini10-Sep-18 11:04 
GeneralRe: Unions in C# Pin
Eytukan12-Sep-18 19:26
Eytukan12-Sep-18 19:26 
GeneralRe: Unions in C# Pin
CPallini12-Sep-18 23:54
mveCPallini12-Sep-18 23:54 
GeneralRe: Unions in C# Pin
englebart11-Sep-18 2:07
professionalenglebart11-Sep-18 2:07 
GeneralRe: Unions in C# Pin
DSewhuk11-Sep-18 8:21
DSewhuk11-Sep-18 8:21 
GeneralRe: Unions in C# Pin
Jerry W. Manweiler, Ph.D.11-Sep-18 11:31
Jerry W. Manweiler, Ph.D.11-Sep-18 11:31 
GeneralRe: Unions in C# Pin
Eytukan12-Sep-18 19:26
Eytukan12-Sep-18 19:26 
GeneralData Dictionaries Pin
NeverJustHere9-Sep-18 23:42
NeverJustHere9-Sep-18 23:42 
GeneralRe: Data Dictionaries Pin
dan!sh 10-Sep-18 0:19
professional dan!sh 10-Sep-18 0:19 
GeneralRe: Data Dictionaries Pin
#realJSOP10-Sep-18 2:30
professional#realJSOP10-Sep-18 2:30 
GeneralRe: Data Dictionaries Pin
Mycroft Holmes10-Sep-18 14:35
professionalMycroft Holmes10-Sep-18 14:35 
GeneralRe: Data Dictionaries Pin
englebart11-Sep-18 2:15
professionalenglebart11-Sep-18 2: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.


Straw Poll

Were you affected by the geomagnetic storms this past weekend?
Communication disruptions, electrified pipes, random unexplained blue-screens in Windows - the list of effects is terrifying.
  Results   39 votes