<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; Uncommon C#</title>
	<atom:link href="http://www.dijksterhuis.org/category/csharp/uncommon-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Uncommon C# : Using C style unions in C#</title>
		<link>http://www.dijksterhuis.org/uncommon-style-unions/</link>
		<comments>http://www.dijksterhuis.org/uncommon-style-unions/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 02:16:47 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Uncommon C#]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=672</guid>
		<description><![CDATA[
A union is a data structure that stores several different types of data at the same single location in memory. Although this might sound like madness in a garbage collected world many older binary data formats still heavily rely on this. A possible use is if you want to modify an IPv4 address. An IPv4 [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2009/02/rings1.jpg" alt="Uncommon unions in C#" title="Uncommon unions in C#" width="500" height="226" class="alignnone size-full wp-image-676" /></p>
<p><em>A union is a data structure that stores several different types of data at the same single location in memory. Although this might sound like madness in a garbage collected world many older binary data formats still heavily rely on this. A possible use is if you want to modify an IPv4 address. An IPv4 address is 4 bytes, or an unsigned C# int. You might want to manipulate the IP address as a single value, or by splitting it into four separate bytes.</em></p>
<p><span id="more-672"></span></p>
<p>C# does not have a union keyword like C or C++ but it is still possible to create the same result by using the StructLayout(LayoutKind.Explicit) and FieldOffset attributes which are part of System.Runtime.InteropServices (which as the name suggests supplies code for working with unmanaged code).</p>
<p>C# allows for three distinct ways of layout data structures in memory: Auto, Explicit, and Sequential. The default is &#8220;Auto&#8221;, but for this to work we need &#8220;Explicit&#8221;. </p>
<p>The FieldOffset sets the physical position of fields within the representation of a class or structure. But what it also allow you to do is to set the same position twice. In the below example we will let the unsigned int for the IP address start at the same spot as the most significant byte.</p>
<pre class="brush: c#">
using System;
using System.Runtime.InteropServices;

namespace ipv4
{

    [StructLayout(LayoutKind.Explicit)]
    struct IPv4Address
    {
        [FieldOffset(0)]
        public uint Address;
        [FieldOffset(0)]
        public byte b3;
        [FieldOffset(1)]
        public byte b2;
        [FieldOffset(2)]
        public byte b1;
        [FieldOffset(3)]
        public byte b0;
    }

    class MainClass
    {
        public static void Main(string[] args)
        {
            IPv4Address myAddress;

            // Assign localhost to the IPv4 address
            myAddress.Address = 0; // Avoid CS0170: Unassigned Field error
            myAddress.b0 = 127;
            myAddress.b1 = 0;
            myAddress.b2 = 0;
            myAddress.b3 = 1;

            Console.WriteLine(&quot;The address in hexadecimal: {0:x}&quot;,myAddress.Address);
        }
    }
}
</pre>
<p>Image credit: <a rel="nofollow" href="http://www.flickr.com/photos/jeffbelmonte/">Jeff Belmonte</a></p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/uncommon-style-unions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
