Tuesday, November 15, 2005

Bacon Grease on the Brain and Union in C#

Bibble, Babble, Dribble, Drabble.

That's what most of this blog is, like bacon grease for the brain. Some people see clogged arteries, messy grease, and other people like me think, hey, this stuff (bacon grease) would make a great salad dressing.

So out of shame or guilt I thought I'd write something technical and shamelessly plug my upcoming book "C# Express for the Professional Programmer".

Did you know you can implement the C/C++ union construct in C#. To do so define a struct with the StructLayoutAttribute(LayoutKind.Explicit) and tag each field with the FieldPositionAttribute(0) value. The result is that such tagged fields have the same memory space. For instance, given a struct with and int i and char c, assigning 65 to i will implicitly assign 'A' to the char. (Didn't know that 65 is ASCII 'A'? Yikes! I am getting old.)

Occasionally union is useful. Peter Norton did some neat things with his File Allocation Table defragmenter programs in the early 90s. (Don't know what a FAT is? ~sigh~)

Comments: Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]