<?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>out &#62;&#62; m_Conscientia; &#187; boxing</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/boxing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Mon, 02 Aug 2010 22:30:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Collections</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/09/collections/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/09/collections/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 20:32:17 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[boxing]]></category>
		<category><![CDATA[Collections]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=451</guid>
		<description><![CDATA[The overarching design of System.Collections is obscured by the poorly conceived nomenclature (others concur). Concrete collections in .NET are based on these key interfaces: IEnumerable simply provides a method to return an IEnumerator. IEnumerator provides the mechanism for forward only iteration. ICollection extends IEnumerable adding some basic machinery for thread safe access and a Count [...]]]></description>
			<content:encoded><![CDATA[<p>The overarching design of <a href="http://msdn.microsoft.com/en-us/library/system.collections.aspx">System.Collections</a> is obscured by the poorly conceived nomenclature (<a href="http://aspnetresources.com/blog/dotnet_collection_madness.aspx">others</a> concur). Concrete collections in .NET are based on these key interfaces:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx">IEnumerable</a> simply provides a method to return an <a href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx">IEnumerator</a>. IEnumerator provides the mechanism for forward only iteration.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.icollection.aspx">ICollection</a> extends IEnumerable adding some basic machinery for thread safe access and a Count property. Note that ICollection does not provide any way of adding/removing elements.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx">IList</a> extends ICollection to support indexing and the facility to add and remove elements. ILists can be fixed size and/or read only.</li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.collections.idictionary.aspx">IDictionary</a> extends ICollection to support  key value pairs. Values may be added, retrieved and removed by key. Both the keys and the values can be retreived as ICollections. IDictionarys can be fixed size and/or read only.</li>
</ul>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx">ArrayList</a> : <a href="http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx">IList</a></strong></p>
<p>ArrayList provides an IList interface over an array which is dynamically resized as you insert elements. Iterating over an ArrayList returns items in the order they were inserted (unless it has been sorted).</p>
<p>The following snippet demonstrates adding, inserting and removing elements and ranges of elements.</p>

<div class="wp_codebox"><table><tr id="p4512"><td class="code" id="p451code2"><pre class="csharp" style="font-family:monospace;">ArrayList al <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ArrayList<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
al<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;this&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Add range requires an ICollection arg</span>
<span style="color: #6666cc; font-weight: bold;">string</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> strs <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;is&quot;</span>, <span style="color: #666666;">&quot;a&quot;</span>, <span style="color: #666666;">&quot;list&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
al<span style="color: #008000;">.</span><span style="color: #0000FF;">AddRange</span><span style="color: #008000;">&#40;</span>strs<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
al<span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #666666;">&quot;hello&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> more <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;non&quot;</span>, <span style="color: #666666;">&quot;sense&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
al<span style="color: #008000;">.</span><span style="color: #0000FF;">InsertRange</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, more<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
al<span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveAt</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
al<span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveRange</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Note that runtime performance is similar to what you might expect if manipulating a raw array. For example, in the worst case, InsertRange is an O(<em>m</em> + <em>n</em>) operation, where <em>n</em> is the number of elements to be added and <em>m</em> is the number of elements in the ArrayList.</p>
<p><strong>Casting and boxing</strong></p>
<p>System.Collections classes come from a time before .NET generics. As a consequence, collection elements are <a href="http://msdn.microsoft.com/en-us/library/system.object.aspx">objects</a> &#8211; everything is cast to/from object as it is stored/retrieved. Value types also get boxed/uboxed. This is all additional overhead.</p>
<p>Contary to what it says in the Framework Foundation book, generics are *sometimes* faster because they avoid this casting and boxing (depending on your use case). Rico Mariani has an interesting <a href="http://blogs.msdn.com/ricom/archive/2005/08/25/performance-quiz-7-generics-improvements-and-costs.aspx">performance comparison</a> (the real killer is the repeated allocation of the ArrayList Enumerator).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/09/collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET Framework Fundamentals</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/07/net-framework-fundamentals/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/07/net-framework-fundamentals/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 18:50:56 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[boxing]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=120</guid>
		<description><![CDATA[Some time ago I began studying for the .NET Framework 2.0 Application Development Foundation exam. In order to cement the aquired knowledge I have decided to post my notes as I recap, starting at the absolute beginning. Types For some time I didn&#8217;t fully appreciate the difference between reference and value types. The following table [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I began studying for the .NET Framework 2.0 Application Development Foundation exam. In order to cement the aquired knowledge I have decided to post my notes as I recap, starting at the absolute beginning.</p>
<p><strong>Types</strong></p>
<p>For some time I didn&#8217;t fully appreciate the difference between reference and value types. The following table summarises the important characteristics:</p>
<table border="0" cellspacing="1" cellpadding="1" width="100%">
<tbody>
<tr>
<th scope="col"><a href="http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx">Value Types</a></th>
<th scope="col"><a href="http://msdn.microsoft.com/en-us/library/490f96s2.aspx">Reference Types</a></th>
</tr>
<tr>
<td>Actually contain data. Assignment copies data.</td>
<td>Store a reference to the real data, assignment copies the reference.</td>
</tr>
<tr>
<td>Local value types reside on the stack.*</td>
<td>Reference resides on the stack, the actual data on the heap.</td>
</tr>
<tr>
<td>Derived (implicitly) from System.ValueType</td>
<td>Derived from System.Object, or a descendant</td>
</tr>
<tr>
<td>User value types are structs or enums. Unlike in C, you can define methods on structs.</td>
<td>May be a class, interface, delegate or an array (including arrays of value types).</td>
</tr>
<tr>
<td>Implicitly sealed</td>
<td></td>
</tr>
</tbody>
</table>
<p>* Local as in declared within a method. When allocating a class with a value type member, this member will be allocated on the heap.</p>
<p><strong>Boxing and Unboxing</strong></p>
<p>A boxing operation converts a value type to a reference type; unboxing converts a reference type to a value type. For example, converting an int to an object (boxing) causes a new int to be created on the heap, the object reference then refers to this copy. Clearly, this incurs some overhead.</p>
<p><strong>Generics</strong></p>
<p>Generics are paramaterised types and methods, and are superficially <a href="http://msdn.microsoft.com/en-us/library/c6cyy67b.aspx">similar to C++ templates</a>. The most common use of generics in the .NET framework are the collection classes introduced in .NET 2.0.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/d5x73970.aspx ">Constraints</a> may be specified to restrict use of generic types and methods: </p>
<table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td>where T : struct</td>
<td>any value type, excluding Nullable types</td>
</tr>
<tr>
<td>where T : class</td>
<td>any reference type (class, interface, delegate or array)</td>
</tr>
<tr>
<td>where T : new</td>
<td>T must have a public parameterless constructor</td>
</tr>
<tr>
<td>where T : &lt;base&gt;</td>
<td>T must derive from base, which can be either a class or interface. Multiple interfaces may be specified, and they may also be generic.</td>
</tr>
</table>
<p><strong>Attributes</strong></p>
<p><a href="http://msdn.microsoft.com/en-us/library/system.attribute.aspx ">Attributes</a> are programmatic annotations (metadata) that may be queried by reflection. An item decorated with an attribute can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute. Attributes derive from System.Attribute.</p>
<p>The TypeForwardedTo attribute allows you to move a type from one assembly to another without having to recompile the application which uses the original assembly. This is succinctly illustrated <a href="http://blogs.msdn.com/thottams/archive/2006/11/17/typeforwarding-typeforwardedto-attribute-in-runtime-compilerservices.aspx">here</a>.</p>
<p><strong>Partial Classes</strong></p>
<p>Partial Classes facilitate splitting class definitions across files. I find this is useful when using code generation – you can re-generate an entire class file from a model without overwriting hand written code.</p>
<p><strong>Exceptions</strong></p>
<p>The training material recommends that you define your own exceptions which derive from System.ApplicationException. It is therefore possible to differentiate between exceptions thrown from application code and the .NET framework.</p>
<p>In reality, it is best practice to simply inherit from Exception, <a href="http://stackoverflow.com/questions/52753/derive-from-exception-or-applicationexception-in-net">as ApplicationException didn’t add any value</a>.</p>
<p>Either way, having distinct exception classes allows you to respond to different exceptions differently. Since it is possible to catch a derived exception by catching the base type, catch blocks should be ordered from most to least specific.</p>
<p>The finally block should be used to dispose of non memory resources, e.g. closing files or a SqlConnection.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/07/net-framework-fundamentals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
