<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>General C# Programming</title>
        <link>http://geekswithblogs.net/akraus1/category/3939.aspx</link>
        <description>General C# Programming</description>
        <language>en-US</language>
        <copyright>Alois Kraus</copyright>
        <managingEditor>akraus1@gmx.de</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Create Strong Named Assemblies - The Easy Way</title>
            <link>http://geekswithblogs.net/akraus1/archive/2007/01/23/104288.aspx</link>
            <description>Strong naming has always been a complicated matter.&amp;nbsp; Sometimes it is even impossible when you get unsigned assemblies from a third party software vendor and you have to build strong named assemblies to install them into the GAC. When you decide to strong name your project it is a all or nothing decision. If your target is strong named then all&amp;nbsp;references must also be strong named. If you do not have the source code and a project for your third party dll you will not be able to sign your project. I have created&amp;nbsp;&lt;A href="http://www.codeplex.com/Signer/Release/ProjectReleases.aspx"&gt;Signer&lt;/A&gt; which is hosted at CodePlex that allows you to add strong names to binaries within minutes. You can even create a strong named build of your whole project without any changes to your build settings. Below is a picture which shows Signer in action:&lt;BR&gt;&lt;BR&gt;&lt;IMG title=Signer style="WIDTH: 989px; HEIGHT: 559px" alt=Signer src="/images/geekswithblogs_net/akraus1/3938/o_Signer.JPG"&gt;&lt;BR&gt;&lt;BR&gt;
&lt;H2&gt;How does it work?&lt;/H2&gt;Signer does basically a full round trip by decompiling the assembly into IL code make the necessary modifications and compile it back to a valid assembly. The required modifications include&lt;BR&gt;
&lt;UL&gt;
&lt;LI&gt;Update of all references&lt;/LI&gt;
&lt;LI&gt;Change/Removal of InternalsVisibleToAttribute&lt;/LI&gt;
&lt;LI&gt;Update of custom attributes with a type parameter&lt;/LI&gt;
&lt;LI&gt;A little fix to work around an ILDASM problem&lt;/LI&gt;&lt;/UL&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Reference Update in IL&lt;BR&gt;&lt;/SPAN&gt;&lt;BR&gt;This is the easy part. A reference does consist of the assembly name, public key token and it's version. Signer has only to insert the public key token where none is present to patch the IL file.&lt;BR&gt;&lt;BR&gt;&lt;SMALL&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;.assembly extern Microsoft.Practices.EnterpriseLibrary.Common&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;{&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp; &lt;SPAN style="FONT-WEIGHT: bold"&gt;.publickeytoken = (BE CA 05 5E 5B 7D 2D C8 ) // Inserted by Signer if not present&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp; .ver 2:0:0:0&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;/SMALL&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;SMALL&gt;}&lt;/SMALL&gt;&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;InternalsVisibleTo Attribute&lt;BR&gt;&lt;/SPAN&gt;&lt;BR&gt;With .NET 2.0 the &lt;A href="http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx"&gt;InternalsVisibleTo&lt;/A&gt; attribute was introduced that allows to grant other assemblies access to classes and methods marked with the internal keyword. To prevent in a strong name scenario that an unsigned assembly does get access to internals directly (you can use reflection instead but this comes with a perf cost) the CLR does check at runtime if all InternalsVisibleTo attribute declarations grant access to strong named assemblies only. If not a runtime exception is thrown and the process terminates.&lt;BR&gt;&lt;BR&gt;&lt;SMALL&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;.custom&amp;nbsp; instance void [mscorlib]System.Runtime.CompilerServices.InternalsVisibleToAttribute::.ctor(string)&amp;nbsp; = &lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;( 01 00 48 4D 69 63 72 6F 73 6F 66 74 2E 50 72 61&amp;nbsp;&amp;nbsp; // ..HMicrosoft.Pra&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; 63 74 69 63 65 73 2E 45 6E 74 65 72 70 72 69 73&amp;nbsp;&amp;nbsp; // ctices.Enterpris&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 65 4C 69 62 72 61 72 79 2E 4C 6F 67 67 69 6E 67&amp;nbsp;&amp;nbsp; // eLibrary.Logging&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2E 43 6F 6E 66 69 67 75 72 61 74 69 6F 6E 2E 44&amp;nbsp;&amp;nbsp; // .Configuration.D&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 65 73 69 67 6E 2E 54 65 73 74 73 00 00 )&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // esign.Tests.."&lt;/SPAN&gt;&lt;/SMALL&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;BR&gt;You have to insert the public key which is over 300 bytes long into your project which is quite tedious if you want to do it manually. At the moment Signer makes life easy and simply removes the attribute to prevent run time errors but if needed the change is trivial. &lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Custom Attributes&lt;/SPAN&gt;&lt;BR&gt;This was by far the most tricky part. When you declare custom attributes with a type parameter the full qualified type name including assembly and public key token is inserted into the IL code. Normally you get this as a binary blob but with the /CAVERBAL option of ILDASM which was introduced with .NET 2.0 things get much easier.&lt;BR&gt;&lt;BR&gt;&lt;SMALL&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;.custom instance void [System]System.ComponentModel.EditorAttribute::.ctor(class [mscorlib]System.Type,&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; class [mscorlib]System.Type)&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; = {&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;type(class&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 'Microsoft.Practices.EnterpriseLibrary.Configuration.Design.ReferenceEditor,&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Microsoft.Practices.EnterpriseLibrary.Configuration.Design, Version=2.0.0.0, Culture=neutral,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PublicKeyToken=beca055e5b7d2dc8')&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;type(class &lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;'System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral,&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; PublicKeyToken=b03f5f7f11d50a3a')&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;/SMALL&gt;&lt;BR&gt;Signer does parse the attributes and does set the PublicKeyToken where needed.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;ILDASM Problems&lt;/SPAN&gt;&lt;BR&gt;All would work wonderful if, well if the nice /CAVERBAL option would produce running code. But I got many ConfiguratonErrors exceptions which told me that the default value was of the wrong type. Arghhh. After digging a little deeper I found out what was going on. Suppose you have a simple configuration class for settings inside the App.Config file:&lt;BR&gt;&lt;BR&gt;
&lt;STYLE type=text/css&gt;
.cf { font-family: Courier New; font-size: 10pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: teal; }
.cb3 { color: maroon; }
.cb4 { color: green; }
&lt;/STYLE&gt;

&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&lt;SPAN class=cb1&gt;using&lt;/SPAN&gt; System;&lt;/P&gt;
&lt;P class=cl&gt;&lt;SPAN class=cb1&gt;using&lt;/SPAN&gt; System.Collections.Generic;&lt;/P&gt;
&lt;P class=cl&gt;&lt;SPAN class=cb1&gt;using&lt;/SPAN&gt; System.Text;&lt;/P&gt;
&lt;P class=cl&gt;&lt;SPAN class=cb1&gt;using&lt;/SPAN&gt; System.Configuration;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&lt;SPAN class=cb1&gt;namespace&lt;/SPAN&gt; NoRoundTrip&lt;/P&gt;
&lt;P class=cl&gt;{&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;enum&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;MyValues&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; V1&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;class&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;Check&lt;/SPAN&gt; : &lt;SPAN class=cb2&gt;ConfigurationSection&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;const&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;string&lt;/SPAN&gt; PropName = &lt;SPAN class=cb3&gt;"Name"&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb4&gt;// DefaultValue is a property of type object&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;SPAN class=cb2&gt;ConfigurationProperty&lt;/SPAN&gt;(PropName,DefaultValue=&lt;SPAN class=cb2&gt;MyValues&lt;/SPAN&gt;.V1)]&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;MyValues&lt;/SPAN&gt; Value&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;get&lt;/SPAN&gt; { &lt;SPAN class=cb1&gt;return&lt;/SPAN&gt; (&lt;SPAN class=cb2&gt;MyValues&lt;/SPAN&gt;) &lt;SPAN class=cb1&gt;this&lt;/SPAN&gt;[PropName]; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;class&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;Program&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;void&lt;/SPAN&gt; Main(&lt;SPAN class=cb1&gt;string&lt;/SPAN&gt;[] args)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Check&lt;/SPAN&gt; c = &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;Check&lt;/SPAN&gt;();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2 style="COLOR: rgb(255,0,0); BACKGROUND-COLOR: rgb(255,255,255)"&gt;MyValues&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(255,0,0); BACKGROUND-COLOR: rgb(255,255,255)"&gt; defValue = c.Value;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN class=cb4 style="COLOR: rgb(255,0,0); BACKGROUND-COLOR: rgb(255,255,255)"&gt;// get default value&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;The program will work without problem when used as is. But after decompiling with the /CAVERBAL option and recompiling you will get a nasty runtime exception:&lt;BR&gt;&lt;BR&gt;&lt;SMALL&gt;&lt;SMALL&gt;&lt;BIG style="FONT-WEIGHT: bold"&gt;&lt;BIG&gt;Unhandled Exception: System.Configuration.ConfigurationErrorsException: &lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;The default value for the property 'Name' has different type than the one of the property itself.&lt;/BIG&gt;&lt;/BIG&gt;&lt;BR&gt;&lt;/SMALL&gt;&lt;/SMALL&gt;&lt;BR&gt;Here is the innocent looking IL code &lt;BR&gt;&lt;SMALL&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .custom instance void&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [System.Configuration]System.Configuration.ConfigurationPropertyAttribute::.ctor(string)&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = {&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; string('Name')&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: rgb(255,0,0)"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; property object 'DefaultValue' = object( int32(0) )&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;}&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;/SMALL&gt;&lt;BR&gt;The ConfigurationProperty DefaultValue does not preserve type identity for enums but does simply declare them as its native integer value which will lead to this exception during run time because there is an extra check code in the BCL code. The generated IL code&amp;nbsp;is valid for the CLR but not for the BCL. Signer does get around this issue by decompiling every assembly&amp;nbsp;once with the parser friendly /CAVERBAL option and the second time without. It will then merge the binary blobs for the ConfigurationProperty Attributes into the other IL file to get a fully functional assembly. Once this obstacle was solved all worked perfectly. &lt;BR&gt;&lt;BR&gt;
&lt;H2&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Signer Usage&lt;/SPAN&gt;&lt;/H2&gt;If you start Signer without any options you will get a pretty self explanatory help to do signing. The only thing to remeber is that the .NET Framework tool sn &amp;nbsp;is needed to generate a strong name key pair by simply executing sn -k c:\key.snk and you are ready to use Signer out of the box with your project.&lt;BR&gt;&lt;BR&gt;&lt;SMALL&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;Signer (C) by Alois Kraus 2007 (akraus1@gmx.de) version 1.0.0.0&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;Signer -k &amp;lt;KeyFile&amp;gt; -outdir &amp;lt;Output Dir&amp;gt; -a &amp;lt;Assembly1&amp;gt; &amp;lt;Assembly2&amp;gt; ... [-decomp "Options" -comp "Options" -debug]&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp; Signer does sign all assemblies and updates their references if the assembly is not already signed&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp; This way you can convert a whole bunch of assemblies which have never seen a key file into strong named assemblies without recompiling!&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp; -k&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Key File name of key container file which is created usually by the sign tool (sn -k)&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp; -outdir&amp;nbsp;&amp;nbsp; Output directory where the strong signed assemblies are copied to&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp; -decomp&amp;nbsp;&amp;nbsp; Additional ildasm options e.g. "/STATS"&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp; -comp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Additional ilasm options e.g. "/X64"&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp; -a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Can be a list of assembly names or a wildcard search pattern e.g. *.dll&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp; -debug&amp;nbsp;&amp;nbsp;&amp;nbsp; Do not delete temporary IL files in %TEMP% flder after completion&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;Example: Signer -k c:\key.snk -outdir .\build -a *.dll *.exe&amp;nbsp; Copy the signed assemblies into the newly created directory build.&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;Example: Signer -k c:\key.snk -outdir . -a *.dll *.exe&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sign and overwrite the orignal binaries in current directory.&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;Error: No Key file specifed (use -k KeyFile ).&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;Error: No Output directory specifed (use -outdir Dir ).&lt;/SPAN&gt;&lt;BR style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;Error: No input assemblies specified (use -a Assembly ).&lt;/SPAN&gt;&lt;/SMALL&gt;&lt;BR&gt;&lt;BR&gt;
&lt;H2&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Conclusions&lt;/SPAN&gt;&lt;/H2&gt;It was fun to create the tool while learning quite a lot about the assembly structure, strong names and IL. As you can see strong names are not really a protection for your software as they can be easily removed and your assemblies resigned without big problems. The only real advantage strong names offer is that you can prove that an assembly which has your public key on it was created by you as long as your secret key is kept safe.&lt;BR&gt;From this point other extensions to Signer are quite easy to add. E.g. removal of strong names, make internals public or adding your own InternalsVisibleTo attribute to other assemblies. The world is full of possibilities ;-). &lt;BR&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=104288"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=104288" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/104288.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2007/01/23/104288.aspx</guid>
            <pubDate>Tue, 23 Jan 2007 20:21:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/104288.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2007/01/23/104288.aspx#feedback</comments>
            <slash:comments>30</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/104288.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/104288.aspx</trackback:ping>
        </item>
        <item>
            <title>Cooperative Application Shutdown with the CLR</title>
            <link>http://geekswithblogs.net/akraus1/archive/2006/10/30/95435.aspx</link>
            <description>All good things have to end even your perfectly working managed executable. But do you know in what circumstances the CLR will terminate your program and much more importantly when do you have a chance to run some finalizers and shutdown code? As always in live it depends. Lets have a look at the reasons why program execution can be terminated.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Ways to terminate an application:&lt;/SPAN&gt;&lt;BR&gt;
&lt;OL&gt;
&lt;LI&gt;The last&amp;nbsp; &lt;A href="http://msdn2.microsoft.com/en-us/library/h339syd0.aspx"&gt;foreground&lt;/A&gt; thread of an application ends. The thread which entered the Main function is usually the only&amp;nbsp; foreground thread in your application.&lt;/LI&gt;
&lt;LI&gt;If you run an Windows Forms application you can call &lt;A href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.exit.aspx"&gt;System.Windows.Forms.Application.Exit()&lt;/A&gt; to cause Application.Run to return.&lt;/LI&gt;
&lt;LI&gt;When you call &lt;A href="http://msdn2.microsoft.com/en-us/library/system.environment.exit.aspx"&gt;System.Environment.Exit(nn)&lt;/A&gt;.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Pressing Ctrl-C or Ctrl-Break inside a Console application.&lt;/LI&gt;
&lt;LI&gt;Call &lt;A href="http://msdn2.microsoft.com/en-us/library/system.environment.failfast.aspx"&gt;System.Environment.FailFast&lt;/A&gt; (new in .NET 2.0) to bail out in case of fatal execution errors.&lt;/LI&gt;
&lt;LI&gt;An unhanded exception in any thread regardless if it is managed or unmanaged.&lt;/LI&gt;
&lt;LI&gt;Unmanaged exit calls within unmanaged code.&lt;/LI&gt;&lt;/OL&gt;What exactly happens during shutdown is explained by an excellent article of &lt;A href="http://blogs.msdn.com/cbrumme/archive/2003/08/20/51504.aspx"&gt;Chris Brumme&lt;/A&gt;.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Shutdown Process&lt;/SPAN&gt;&lt;BR&gt;Generally speaking we can distinguish between two shutdown types: Cooperative and Abnormal. Cooperative means that you get some help from the CLR (execution guarantees, callback handlers, ...) while the other form of exit is very rude and you will get&amp;nbsp;no help from the runtime. &lt;BR&gt;During a cooperative shutdown the CLR will unload the Application Domain (e.g. after leaving the Main function). This involves killing all threads by doing a hard kill as opposed to the "normal"&amp;nbsp;&lt;A href="http://msdn2.microsoft.com/en-us/library/system.threading.threadabortexception.aspx"&gt;ThreadAbortException&lt;/A&gt; way where the finally blocks of each thread are executed. In reality the threads to be killed are suspended and never resumed. After all threads sleep the&amp;nbsp;pending&amp;nbsp;finalizers are executed inside the Finalizer thread. Now comes a new type of finalizers into the game which was introduced with .NET 2.0. The &lt;A href="http://msdn2.microsoft.com/en-us/library/system.runtime.constrainedexecution.criticalfinalizerobject.aspx"&gt;Critical Finalizers&lt;/A&gt; are guaranteed to be called even when normal finalization did timeout (for .NET 2.0 the time is 40s&amp;nbsp;) and further finalizer processing is aborted.&amp;nbsp;If you trigger an exception inside a finalizer and it is not catched you will stop any further finalization processing, including the new Critical Finalizers. This behavior should be changed in a future version of the CLR. What are these "safe" finalizers good for when an "unsafe" finalizer can prevent them from running? There is already a separate critical finalizer queue inside the CLR which is processed after the normal finalization process did take place. &lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Normal Shutdown&lt;/SPAN&gt;&lt;BR&gt;A fully cooperative shutdown is performed in case of 1, 2 and 3. All managed threads are terminated without further notice but all finalizers are executed. No further notice means that no finally blocks are executed while terminating the thread.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Abnormal Shutdown&lt;/SPAN&gt;&lt;BR&gt;The bets are off and no CLR event is fired which could trigger a cleanup action such as calling finalizer, or some events. Case 4 can be converted into a normal shutdown by the code shown below. Case 6 can be partially handled inside the AppDomain.UnhandledException. The other ones (Environment.FailFast and unmanaged exits) have no knowledge of the CLR and perform therefore a rude application exit. The only exception is the Visual C++ (version 7+) Runtime which&amp;nbsp; is aware of the managed world and calls back into the CLR (CorExitProcess) when you call the unmanaged exit() or abort() functions.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Interesting Events&lt;BR&gt;&lt;/SPAN&gt;All these events are only processed when we are in a cooperative shutdown scenario. Killing your process via the Task Manager will cause a rude abort where none of our managed shutdown code is&amp;nbsp;executed.&lt;BR&gt;
&lt;UL&gt;
&lt;LI&gt;The &lt;A href="http://msdn2.microsoft.com/en-us/library/system.appdomain.domainunload.aspx"&gt;AppDomain.DomainUnload&lt;/A&gt; &amp;nbsp;event is not called in your default AppDomain. When you exit e.g. the Main function the default AppDomain is unloaded but your event handler will never be called. The DomainUnload handler is called from an arbitrary thread but always in the context of the domain that is about to be unloaded.&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://msdn2.microsoft.com/en-US/library/system.appdomain.processexit.aspx"&gt;AppDomain.ProcessExit&lt;/A&gt; is called when the CLR does detect that a shutdown has to be performed. You can register in each AppDomain such a handler but be reminded that after the DomainUnload event was fired you will never see a ProcessExit event because your AppDomain is already gone. These two events are mutually exclusive. When you kill the process via&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx"&gt;AppDomain.UnhandledException&lt;/A&gt; is called only in the default AppDomain. You can register this handler only in the "root" AppDomain which is the first one created for you by the CLR. Hosting of other AppDomains in a plugin architecture can cause unexpected side effects if the loaded modules register this handler and expect it to be called. When the handler is left the process finally terminates which cannot be prevented in .NET 2.0 except if you set a &lt;A href="http://msdn2.microsoft.com/en-us/library/ms228965.aspx"&gt;compatibility flag&lt;/A&gt; inside the runtime settings your App.config file. &lt;PRE class=code id=ctl00_LibFrame_ctl12other space="preserve"&gt; &amp;lt;legacyUnhandledExceptionPolicy enabled="1"/&amp;gt;&lt;/PRE&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Ctrl-C/Ctrl-Break&lt;/SPAN&gt;&lt;BR&gt;The default behavior of the CLR is to do nothing. This does mean that the CLR is notified very late by a DLL_PROCESS_DETACH notification in which context no managed code can run anymore because the OS loader lock is already taken. We are left in the unfortunate situation that we get no notification events nor are any finalizers run. All threads are silently killed without a chance to execute their catch/finally blocks to do an orderly shutdown. I said in the first sentence default because there is a way to handle this situation gracefully. The Console class has got a new event member with .NET 2.0: &lt;A href="http://msdn2.microsoft.com/en-us/library/system.console.cancelkeypress.aspx"&gt;Console.CancelKeyPress&lt;/A&gt;. it allows you to get notified of Ctrl-C and Ctrl-Break keys where you can stop the shutdown (only for Ctrl-C but not Ctrl-Break). If you want to do a coordinated shutdown inside the handler you will run into the gotcha that calling Environment.Exit does not trigger any finalizers. When we do have other plans to exit the process we need to spin up a little helper thread inside the event handler which will then call Environment.Exit. Voila our finalizers are called.&lt;BR&gt;&lt;BR&gt;
&lt;HR style="WIDTH: 100%; HEIGHT: 2px"&gt;
&lt;SPAN style="FONT-WEIGHT: bold"&gt;Graceful Shutdown when Ctrl-C or Ctrl-Break was pressed.&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;
&lt;STYLE type=text/css&gt;
.cf { font-family: Courier New; font-size: 10pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: teal; }
.cb3 { color: maroon; }
.cb4 { color: green; }
&lt;/STYLE&gt;

&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;void&lt;/SPAN&gt; GraceFullCtrlC()&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Console&lt;/SPAN&gt;.CancelKeyPress += &lt;SPAN class=cb1&gt;delegate&lt;/SPAN&gt;(&lt;SPAN class=cb1&gt;object&lt;/SPAN&gt; sender, &lt;SPAN class=cb2&gt;ConsoleCancelEventArgs&lt;/SPAN&gt; e)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;if&lt;/SPAN&gt;( e.SpecialKey == &lt;SPAN class=cb2&gt;ConsoleSpecialKey&lt;/SPAN&gt;.ControlBreak )&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN class=cb3&gt;"Ctrl-Break catched and translated into an cooperative shutdown"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb4&gt;// Envirionment.Exit(1) would NOT do a cooperative shutdown. No finalizers are called!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Thread&lt;/SPAN&gt; t = &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;Thread&lt;/SPAN&gt;(&lt;SPAN class=cb1&gt;delegate&lt;/SPAN&gt;()&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN class=cb3&gt;"Asynchronous shutdown started"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Environment&lt;/SPAN&gt;.Exit(1);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t.Start();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t.Join();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;if&lt;/SPAN&gt;( e.SpecialKey == &lt;SPAN class=cb2&gt;ConsoleSpecialKey&lt;/SPAN&gt;.ControlC )&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; e.Cancel = &lt;SPAN class=cb1&gt;true&lt;/SPAN&gt;; &lt;SPAN class=cb4&gt;// tell the CLR to keep running&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN class=cb3&gt;"Ctrl-C catched and translated into cooperative shutdown"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb4&gt;// If we want to call exit triggered from out event handler we have to spin&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb4&gt;// up another thread. If somebody of the CLR team reads this. Please fix!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;Thread&lt;/SPAN&gt;(&lt;SPAN class=cb1&gt;delegate&lt;/SPAN&gt;()&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN class=cb3&gt;"Asynchronous shutdown started"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Environment&lt;/SPAN&gt;.Exit(2);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }).Start();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN class=cb3&gt;"Press now Ctrl-C or Ctrl-Break"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Thread&lt;/SPAN&gt;.Sleep(10000);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;
&lt;HR style="WIDTH: 100%; HEIGHT: 2px"&gt;
&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Unhandled Exceptions&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;It is very easy to screw up your application with an abnormal&amp;nbsp;application exit. Lets suppose the following code:&lt;BR&gt;&lt;BR&gt;
&lt;STYLE type=text/css&gt;
.cf { font-family: Courier New; font-size: 10pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: green; }
.cb3 { color: teal; }
.cb4 { color: maroon; }
&lt;/STYLE&gt;

&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;void&lt;/SPAN&gt; RudeThreadExitExample()&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;// Create a new thread&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb3&gt;Thread&lt;/SPAN&gt;(&lt;SPAN class=cb1&gt;delegate&lt;/SPAN&gt;()&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN class=cb4&gt;"New Thread started"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;// throw an unhandled exception in this thread which will cause the application to terminate&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;// without calling finalizers and any other code. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;throw&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb3&gt;Exception&lt;/SPAN&gt;(&lt;SPAN class=cb4&gt;"Uups this thread is going to die now"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }).Start();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&lt;/P&gt;&lt;/DIV&gt;We spin up an additional thread and create an unhandled exception. It is possible to register the AppDomain.Unhandled exception handler where you can e.g. log the exception but nonetheless your application will be rudely terminated. No finalizers are executed when an unhandled exception occurs. If you try to fix this by calling Environment.Exit inside your handler nothing will happen except that the ProcessExit event is triggered. We can employ our little Ctrl-C trick here and force an ordered cooperative shutdown from another thread.&lt;BR&gt;
&lt;HR style="WIDTH: 100%; HEIGHT: 2px"&gt;
&lt;SPAN style="FONT-WEIGHT: bold"&gt;Cooperative Unhandled Exception Handler (Finalizers are called)&lt;/SPAN&gt;&lt;BR&gt;
&lt;STYLE type=text/css&gt;
.cf { font-family: Courier New; font-size: 10pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: teal; }
.cb3 { color: maroon; }
.cb4 { color: green; }
&lt;/STYLE&gt;

&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;void&lt;/SPAN&gt; CurrentDomain_UnhandledException(&lt;SPAN class=cb1&gt;object&lt;/SPAN&gt; sender, &lt;SPAN class=cb2&gt;UnhandledExceptionEventArgs&lt;/SPAN&gt; e)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN class=cb3&gt;"Unhandled exception handler fired. Object: "&lt;/SPAN&gt; + e.ExceptionObject);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb4&gt;// Prepare cooperative async shutdown from another thread&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Thread&lt;/SPAN&gt; t = &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;Thread&lt;/SPAN&gt;(&lt;SPAN class=cb1&gt;delegate&lt;/SPAN&gt;()&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN class=cb3&gt;"Asynchronous shutdown started"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Environment&lt;/SPAN&gt;.Exit(1);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t.Start();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; t.Join(); // wait until we have exited&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&lt;/P&gt;&lt;/DIV&gt;This way you can ensure that your application exits in a much cleaner way where all&amp;nbsp;finalizers are called. What I found interesting during my investigation that there seems no time limit to exist how long I want to run the unhandled exception handler. It is therefore possible that you have several unhandled exception handlers running at the same time. An ordered application shutdown can be quite twisted if you want to do it right. Armed with the knowledge from this post you should have a much better feeling what will happen when things go wrong now.&lt;BR&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=95435"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=95435" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/95435.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2006/10/30/95435.aspx</guid>
            <pubDate>Mon, 30 Oct 2006 06:12:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/95435.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2006/10/30/95435.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/95435.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/95435.aspx</trackback:ping>
        </item>
        <item>
            <title>Lazy Vs Eager Init Singletons / Double-Check Lock Pattern</title>
            <link>http://geekswithblogs.net/akraus1/archive/2006/09/10/90804.aspx</link>
            <description>&lt;P&gt;
&lt;STYLE type=text/css&gt;
.cf { font-family: courier new; font-size: 10pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: teal; }
  &lt;/STYLE&gt;
Singletons are the easiest programming pattern to learn which seems to be the reason why it is often used in places where it is inappropriate. They are very straightforward to implement and understand. Just create a static field and return the static instance in a public get property. A Logger&amp;nbsp;Singleton is a classic example of this pattern:&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN class=cb1&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN class=cb2&gt;Logger&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;LogWriter&lt;/SPAN&gt; writer = &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;LogWriter&lt;/SPAN&gt;();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;LogWriter&lt;/SPAN&gt; Writer&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;get&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;SPAN class=cb1&gt;return&lt;/SPAN&gt; writer; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;
&lt;P class=cl&gt;If you wrote such code before and thought that this is perfectly working in all cases I must disappoint you. There are subtle things going on when static fields are initialized by the CLR. This implementation is&amp;nbsp;thread safe because static fields are initialized only once by the CLR before any method can be called or a static field can be accessed from the outside. More details can be&amp;nbsp;found inside the &lt;A href="http://msdn.microsoft.com/netframework/ecma/"&gt;ECMA-334 C# Language Specification&lt;/A&gt; and &lt;A href="http://www.ecma-international.org/standards/ecma-335/Ecma-335-part-i-iv.pdf"&gt;ECMA-335 Common Language Infrastructure (CLI)&lt;/A&gt;. Now comes the tricky part.&amp;nbsp;When do you think will be LogWriter instance be instantiated? To answer this question let's have a look at two nearly identical Singletons.&lt;/P&gt;
&lt;P class=cl&gt;&lt;/P&gt;
&lt;H2&gt;&lt;A href="http://geekswithblogs.net/akraus1/articles/90803.aspx"&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Show more ...&lt;/SPAN&gt;&lt;/A&gt;&lt;/H2&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=90804"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=90804" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/90804.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2006/09/10/90804.aspx</guid>
            <pubDate>Mon, 11 Sep 2006 01:36:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/90804.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2006/09/10/90804.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/90804.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/90804.aspx</trackback:ping>
        </item>
        <item>
            <title>The Evolution of Languages</title>
            <link>http://geekswithblogs.net/akraus1/archive/2006/06/23/82859.aspx</link>
            <description>&lt;!--StartFragment --&gt;&amp;nbsp;&lt;A href="http://www.flownet.com/gat/"&gt;Ron Garet&lt;/A&gt; did&amp;nbsp;write about his &lt;A href="http://www.flownet.com/gat/jpl-lisp.html"&gt;personal experiences at NASAs JPL&lt;/A&gt; and how Lisp did perform in outer world (deep space) AI robotic projects. He was kinda disappointed by the way Lisp was abandoned in favor of C and Java because of C-Lisp integration problems. I think it would be interesting to have a look at todays programming languages and their possible future.&lt;BR&gt;The programming languages differ in many aspects such as&lt;BR&gt;
&lt;UL&gt;
&lt;LI&gt;Used &lt;A href="http://en.wikipedia.org/wiki/Programming_paradigm"&gt;Programming Paradigm&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Standard Libraries&lt;/LI&gt;
&lt;LI&gt;Syntax&lt;/LI&gt;&lt;/UL&gt;Today the&amp;nbsp;most widely used languages are &lt;A href="http://en.wikipedia.org/wiki/Imperative_programming"&gt;imperative programming languages&lt;/A&gt;. With Microsoft pushing .NET&amp;nbsp;the market for general purpose programming languages is consolidating. Thats&amp;nbsp;a good sign. Why would I need more than one programming language at all? They are different but why are C/C++, Java and C# such a big success while others also well designed ones (e.g. Lisp) are not? The first thing that comes to my mind is ease of use and the effort to become good at it. Java makes it so easy that some&amp;nbsp;think it has made &lt;A href="http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html"&gt;programming too easy&lt;/A&gt;. Perhaps Java is todays BASIC to which we soon will point with the finger and say:&amp;nbsp; "Java has no goto statement. Thats a bad thing". Because it is so easy to learn it is one of the first languages that our kids will be exposed to. That means that the programing language world is dominated by imperative languages with a virtual machine inside it for the next decades. I hope this will result in an&amp;nbsp; ueber language with a basic syntax for all common things&amp;nbsp;(why do I need to learn again and again how to loop over an string array in another language?) and a high quality Base Class Library which can be accessed by any language that does build on top on the used VM. While the syntax already does look similar of C/C++, C# and Java the used programming paradigm is the same. With the introduction of &lt;A href="http://msdn.microsoft.com/data/ref/linq/"&gt;LINQ&lt;/A&gt;, which brings many constructs formerly only seen in functional programming language to the masses, different programming paradigms are supported within the very same language in a natural way.&lt;BR&gt;The convergence of&amp;nbsp;languages which drives us to one syntax, one Base Library and support for every&amp;nbsp; Programming Paradigm will give us the flexibility we need to build even more complex systems in the future. That does sound too good to be true. But what if I need a hand optimized assembly routine for certain constructs? Well you can program today already low level assembly code with managed languages like C#. By analyzing how you need to arrange your looping constructs you can influence the JITer (Just In Time Compiler) to spit out different machine instructions. Greg Young has put up a very nice &lt;A href="http://codebetter.com/blogs/gregyoung/archive/2006/06/11/146343.aspx"&gt;article&lt;/A&gt; how you can basically program assembler by using C#. The ability to write performant code in a very high level language will be the key success factor for a future ueber language which does bring all together. The low level hackers can do their assembly thing and the Java kids have their easy to learn BASIC. To make this happen we will need much more flexibility at the JIT side. Let's suppose you want to write a sophisticated 3D Game using managed code. Since the CPU is not good enough for high speed rendering we could annotate portions of our code to be optimized for the GPU (Graphics Processing Unit) which will execute there. Imagine the possibilities you would have if you could write things like this:&lt;BR&gt;&lt;BR&gt;[JITWith("GraphicsCard_GPU_Module")]&lt;BR&gt;void 3DProcessing()&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; foreach(Point p in Points) &amp;nbsp;// This code is executed on your graphics card!&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Draw(p); &amp;nbsp; &lt;BR&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;By the possibility to extend the JIT compiler we do not have any managed unmanaged transition in our high level code anymore. I believe that the language war is not only fought with syntax and libraries but also at the JIT level. Perhaps we have in some years one common language syntax and many&amp;nbsp;JIT extensions to create highly optimized domain specific code. &lt;BR&gt;To have only one syntax&amp;nbsp;is surely whisful thinking (but I like the idea) as it turns out that many complicated (read algorithmic) problems are not easy to implement with mainstream languages because unorthodox solutions will usually collide with good programming practices and the syntax. A smart syntax that is very expressive and does not impose much structure to your program (like enforcing OO) does attract&amp;nbsp;people which&amp;nbsp;write algorithms. So far I have (please correct me if I am wrong) seen the functional programming paradigm mainly in the field of:&amp;nbsp; &lt;BR&gt;
&lt;UL&gt;
&lt;LI&gt;Parsers&lt;/LI&gt;
&lt;LI&gt;Compilers&lt;/LI&gt;
&lt;LI&gt;Algorithms&lt;/LI&gt;&lt;/UL&gt;The less structured syntax elements a language demands the easier it is to change the implementation of an algorithm totally because the structure of your algorithm does dictate&amp;nbsp;the overall structure of your code and not the syntax of the language&amp;nbsp;(e.g. C++ by enforcing OO where it does simply not fit)&amp;nbsp;. In a highly flexible development environment (e.g. robotic research) where radically new ideas are constantly integrated into your code base the functional approach does work very good. But the lack of syntactic structure makes it hard to coordinate the work of many people which do usually work on&amp;nbsp;a small component of a large project. This is where information hiding the and the success of Object Oriented Design comes into the game which brings us nearer to the goal of truly reusable components.&amp;nbsp;Languages emerged to help us to&amp;nbsp;support OO design intrinsically by enforcing it inside their&amp;nbsp;syntax (C++, Java, C#, ...) . With &lt;A href="http://www.sei.cmu.edu/productlines/frame_report/what.is.a.PL.htm"&gt;Software Product Lines&lt;/A&gt;&amp;nbsp;an old idea has made it into daily&amp;nbsp;business that does create&amp;nbsp;reusable components which are assembled into a commercial product. To make the product line idea work you need common stable interfaces which do not change too much over time. This requires many&amp;nbsp;syntactic elements that hinder your creativity but help to enforce corporate policy at source code level (e.g. Java). No big mental abilities are needed for this, only a certain degree of discipline and the ability to judge where you should cut your program into smaller components to keep it maintainable. From this point of view I think we should be able to do very well with only two general purpose programming languages where one &amp;nbsp;does support the component fraction whereas the other does help the quick and dirty prototypers. DSL (Domain Specific Languages) could be integrated as LINQ already shows us today into both to get a solid syntactic foundation that could much longer than todays rapid language evolution. &lt;A href="http://stal.blogspot.com/"&gt;Michael Stal&lt;/A&gt; has posted a nice article about his point of view how DSLs could be integrated into general purpose programming languages.&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=82859"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=82859" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/82859.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2006/06/23/82859.aspx</guid>
            <pubDate>Sat, 24 Jun 2006 02:19:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/82859.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2006/06/23/82859.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/82859.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/82859.aspx</trackback:ping>
        </item>
        <item>
            <title>Why .NET Tracing is not reliable</title>
            <link>http://geekswithblogs.net/akraus1/archive/2006/06/12/81630.aspx</link>
            <description>&lt;P&gt;&lt;!--StartFragment --&gt;&amp;nbsp;&lt;SPAN id=nsrTitle&gt;&lt;/SPAN&gt;This time I wanted to write something about the .NET Framework that is solved not optimal of the .NET Framework. My unsuspecting victim is the well known System.Diagnostics.Trace class. Static classes have the big plus that they are easy to use but they can be very hard to maintain if you want to extend it (no derivation) and have a lifetime problem with the objects that are created inside it. The Trace facade is in my opinion a prominent example of &amp;nbsp;how you should not design a reliable static facade class. It's has many features that are nice but it's at the same time a fine example of design that has lost its vision. It tries to do too many things at the same time without succeeding in the optimal solution for all the goals it might have. Tracing should be fast, right? The .NET Trace definitely not the fastest solution. Yes it's flexible and can be very good configured inside your App.config file. But if you have used the &lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyId=C8CA14D0-05EA-4A44-AE78-F5E4DF6208AF&amp;amp;displaylang=en"&gt;Microsoft Logging Application Block&lt;/A&gt; of &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/entlib.asp"&gt;Enterprise Library 2.0&lt;/A&gt; once then you know how much farther&amp;nbsp;design for flexibility can be done. But I must admit that the Entlib TraceListeners does derive from the .NET ones. Below is a +- table about the .NET Trace regarding Performance Reliability and Extensibility.&lt;BR&gt;In the following article I will show you what reliability issues there are and how to solve them.&lt;BR&gt;&lt;BIG&gt;&lt;BR&gt;&lt;/BIG&gt;
&lt;TABLE style="WIDTH: 1110px; HEIGHT: 220px; BACKGROUND-COLOR: rgb(255,255,255); TEXT-ALIGN: left" cellSpacing=2 cellPadding=2 border=1&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH&gt;Performance&lt;/TH&gt;
&lt;TH&gt;Reliability&lt;/TH&gt;
&lt;TH&gt;Extensibility/&lt;BR&gt;Ease of use&lt;/TH&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;SPAN id=nsrTitle&gt;+&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;TraceSwitches allow to configure at run&amp;nbsp; time&amp;nbsp;what&amp;nbsp; should be traced.&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;BIG style="FONT-WEIGHT: bold"&gt;-&lt;/BIG&gt; &lt;SPAN id=nsrTitle&gt;DefaultTraceListener &amp;nbsp;uses OutputDebugString&amp;nbsp; which&amp;nbsp; is by far the slowest method to trace.&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;SPAN id=nsrTitle&gt;&lt;BIG style="FONT-WEIGHT: bold"&gt;&lt;BIG&gt;&lt;SMALL&gt;-&lt;/SMALL&gt;&amp;nbsp;&lt;/BIG&gt;&lt;/BIG&gt;I loose&amp;nbsp;traces if I do not call Trace.Close/Flush&amp;nbsp;from&amp;nbsp;time&amp;nbsp;to time. &lt;BR&gt;&lt;BR&gt;&lt;BIG&gt;-&lt;/BIG&gt; Static Trace.Close is a&amp;nbsp;mistake because I&amp;nbsp;cannot really close&amp;nbsp;something that is static. &lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SMALL&gt;&lt;SPAN id=nsrTitle&gt;&lt;BIG style="FONT-WEIGHT: bold"&gt;&lt;BIG&gt;-&lt;/BIG&gt;&lt;/BIG&gt;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SMALL&gt;&lt;SPAN id=nsrTitle&gt;A Trace.Write after a Trace.Close causes loss of all following traces in case of file based listeners.&lt;BR&gt;&lt;BR&gt;(+ Some Listeners do reopen itself after being closed. The File Listener can do this if you init it with a file name)&lt;BR&gt;&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;SPAN id=nsrTitle&gt;+&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;Dynamic Trace Switch Configuration&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;&lt;BIG style="FONT-WEIGHT: bold"&gt;&lt;BIG&gt;&lt;SMALL&gt;&lt;SMALL&gt;+&lt;/SMALL&gt;&lt;/SMALL&gt; &lt;/BIG&gt;&lt;/BIG&gt;Dynamic Trace Listener Configuration&lt;BR&gt;&lt;BR&gt;&lt;BIG&gt;-&lt;/BIG&gt;&lt;SMALL&gt; &lt;/SMALL&gt;Other (Logging) solutions are far more flexible. &lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;&lt;BIG&gt;-&lt;/BIG&gt; It is nearly impossible to write a program that does&amp;nbsp;&amp;nbsp;capture all traces even during application shutdown. &lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BIG&gt;&lt;BR style="FONT-WEIGHT: bold"&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;&lt;/SPAN&gt;&lt;/BIG&gt;&lt;SPAN id=nsrTitle&gt;&lt;BIG&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;&lt;/SPAN&gt;&lt;/BIG&gt;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;&lt;/SPAN&gt;&lt;SPAN id=nsrTitle&gt;The most obvious&amp;nbsp;reliability problem I did find is demonstrated with a very simple Console Application:&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN id=nsrTitle&gt;&lt;BR&gt;&lt;BIG&gt;&lt;BIG style="FONT-WEIGHT: bold"&gt;&lt;A href="http://geekswithblogs.net/akraus1/articles/81629.aspx"&gt;Show More ...&lt;BR&gt;&lt;/A&gt;&lt;/BIG&gt;&lt;/BIG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=81630"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=81630" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/81630.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2006/06/12/81630.aspx</guid>
            <pubDate>Tue, 13 Jun 2006 02:18:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/81630.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2006/06/12/81630.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/81630.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/81630.aspx</trackback:ping>
        </item>
        <item>
            <title>Functional Programming with F#/C#</title>
            <link>http://geekswithblogs.net/akraus1/archive/2006/05/28/79882.aspx</link>
            <description>&amp;nbsp;After all this hype around C# 3.0 where we will get 
&lt;STYLE type=text/css&gt;
.cf { font-family: Courier New; font-size: 10pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: teal; }
.cb3 { color: maroon; }
.cb4 { color: green; }
  &lt;/STYLE&gt;
&lt;A href="http://msdn.microsoft.com/data/ref/linq/"&gt;LINQ&lt;/A&gt;, lambda expressions and many other thing I thought that it would be useful to have a deeper look at functional programming languages like &lt;A href="http://research.microsoft.com/fsharp/fsharp.aspx"&gt;F#&lt;/A&gt;. At Microsoft quite many people are fond of the&amp;nbsp; &lt;A href="http://en.wikipedia.org/wiki/Functional_programming_language"&gt;Functional Programming&lt;/A&gt;&amp;nbsp;style which did influence the&amp;nbsp;design of C# 3.0. Many language architects there have&amp;nbsp; &lt;A href="http://en.wikipedia.org/wiki/Haskell_programming_language"&gt;Haskell&lt;/A&gt; background which could explain the renaissance of these "old" concepts. So what's the deal with the functional approach? It does basically force you to think in a twisted way where the problem is straight solved by calling a function which does call another function and so on. Lets assume that you want to get the list of all members of all types which are contained in the assemblies that are currently loaded into your AppDomain (to look at the problem this way is the twisted part) you could write for example (in a functional language like F# this problem is very easy to solve):&lt;BR&gt;&lt;PRE&gt;// Assign allMembers the result of our "calculation"&lt;BR&gt;let allMembers = System.AppDomain.CurrentDomain.GetAssemblies()&lt;BR&gt; |&amp;gt; Array.to_list |&amp;gt; List.map (fun a -&amp;gt; a.GetTypes()) |&amp;gt; Array.concat&lt;BR&gt; |&amp;gt; Array.to_list |&amp;gt; List.map (fun ty -&amp;gt; ty.GetMembers()) |&amp;gt; Array.concat&lt;BR&gt;&lt;BR&gt;// Print them out &lt;BR&gt;do Array.foreach allMembers ( fun memberInfo -&amp;gt; printf "\n%s" memberInfo.Name)&lt;/PRE&gt;If you want to understand what this little code fragment&amp;nbsp;really does and how it can be rewritten I recommend the post from &lt;A href="http://strangelights.com/blog/archive/2005/12/28/1277.aspx"&gt;Robert.&lt;/A&gt; A more comprehensible version in&amp;nbsp;C# code would look like this:&lt;BR&gt;
&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN class=cb2&gt;MemberInfo&lt;/SPAN&gt;&amp;gt; GetAllMembers()&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Assembly&lt;/SPAN&gt; [] allAssemblies = &lt;SPAN class=cb2&gt;AppDomain&lt;/SPAN&gt;.CurrentDomain.GetAssemblies();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN class=cb2&gt;Type&lt;/SPAN&gt;&amp;gt; allTypes = &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN class=cb2&gt;Type&lt;/SPAN&gt;&amp;gt;();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;foreach&lt;/SPAN&gt;(&lt;SPAN class=cb2&gt;Assembly&lt;/SPAN&gt; assembly &lt;SPAN class=cb1&gt;in&lt;/SPAN&gt; allAssemblies)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; allTypes.AddRange(assembly.GetTypes());&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN class=cb2&gt;MemberInfo&lt;/SPAN&gt;&amp;gt; allMembers = &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN class=cb2&gt;MemberInfo&lt;/SPAN&gt;&amp;gt;();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;foreach&lt;/SPAN&gt;( &lt;SPAN class=cb2&gt;Type&lt;/SPAN&gt; t &lt;SPAN class=cb1&gt;in&lt;/SPAN&gt; allTypes )&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; allMembers.AddRange( t.GetMembers() );&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;return&lt;/SPAN&gt; allMembers;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;A href="http://geekswithblogs.net/akraus1/articles/79880.aspx"&gt;&lt;BIG&gt;&lt;BIG&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Show more ...&lt;/SPAN&gt;&lt;/BIG&gt;&lt;/BIG&gt;&lt;/A&gt;&lt;BR&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=79882"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=79882" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/79882.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2006/05/28/79882.aspx</guid>
            <pubDate>Sun, 28 May 2006 23:30:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/79882.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2006/05/28/79882.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/79882.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/79882.aspx</trackback:ping>
        </item>
        <item>
            <title>Create Live Videos From Your Screen</title>
            <link>http://geekswithblogs.net/akraus1/archive/2006/05/08/77616.aspx</link>
            <description>&lt;!--StartFragment --&gt;Just recently I found a very good post how to create your own live demo video with &lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyID=5691ba02-e496-465a-bba9-b2f1182cdf24&amp;amp;DisplayLang=en"&gt;Windows Media Encoder&lt;/A&gt;. John Howard did put on his &lt;A href="http://blogs.technet.com/jhoward/archive/2005/01/24/359391.aspx"&gt;blog&lt;/A&gt; &amp;nbsp;a useful post about the preferred settings for a good encoding experience. The main thing is to get the small videos of your computer screen is that you need to set as resolution 800x600 and 16 bit color depth. This is also a very good choice if you want to share your video in a meeting room with a beamer since most of them do not support more than 800x600. After digging a bit further I did find out that I have installed on my Windows XP PC a movie editing software with since Service Pack 2. It is called Movie Maker and resides in your Program Files directory. I have installed so many programs on my PC without knowing anything about it! Scary ... Armed with these tools it is really easy to create a live demo&amp;nbsp;for some presentation which will never fail. No more: "Hm it worked always before". What is even cooler that now everybody can take the demo home and watch the steps at the time he/she does need it. You do not have to do brain surgery to remember what the heck this guy did tell you three weeks ago&amp;nbsp;in his demo to get this thing working. Just open the video and watch it again. This could really become a new trend since it is so easy to create a video demo this way. I found it much easier to show it live&amp;nbsp;than to write&amp;nbsp;(nobody reads it anyway) documentation and later people call you just to ask the question which was answered at page 95 of your famous (though unread) document. Next time I will send them the link to the demo video.&amp;nbsp; &lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=77616"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=77616" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/77616.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2006/05/08/77616.aspx</guid>
            <pubDate>Tue, 09 May 2006 01:28:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/77616.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2006/05/08/77616.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/77616.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/77616.aspx</trackback:ping>
        </item>
        <item>
            <title>Performance Quiz: Fastest way to format time</title>
            <link>http://geekswithblogs.net/akraus1/archive/2006/04/23/76146.aspx</link>
            <description>&lt;P&gt;&lt;!--StartFragment --&gt; We&amp;nbsp;want a good performing application but it is hard to get it right. We all know how to format strings in a fast and efficient way don't we? Let me challenge you if anybody can beat my lightning fast fixed string formatting function. The rules are easy:&lt;BR&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;We want to format the content of DateTime structure into a string of the form: &amp;nbsp;"08:44:45.423"&lt;/LI&gt;
&lt;LI&gt;The performance measured&amp;nbsp;is the time needed to return the formatted string when the DateTime structure is as input given.&lt;/LI&gt;
&lt;LI&gt;.NET 2.0 and C# only.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;For starters I publish here the class which measures the performance&lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;class&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;Stopper&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;delegate&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;string&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;TestFunction&lt;/SPAN&gt;(&lt;SPAN class=cb2&gt;DateTime&lt;/SPAN&gt; time);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; myIterations;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; Iterations&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;get&lt;/SPAN&gt; { &lt;SPAN class=cb1&gt;return&lt;/SPAN&gt; myIterations; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;set&lt;/SPAN&gt; { myIterations = &lt;SPAN class=cb1&gt;value&lt;/SPAN&gt;; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;static&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;void&lt;/SPAN&gt; CheckTime(&lt;SPAN class=cb1&gt;string&lt;/SPAN&gt; action, &lt;SPAN class=cb2&gt;TestFunction&lt;/SPAN&gt; func)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Stopwatch&lt;/SPAN&gt; Watch = &lt;SPAN class=cb2&gt;Stopwatch&lt;/SPAN&gt;.StartNew();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;DateTime&lt;/SPAN&gt; curTime = &lt;SPAN class=cb2&gt;DateTime&lt;/SPAN&gt;.Now;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;for&lt;/SPAN&gt; (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; i = 0; i &amp;lt; Iterations; i++)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; func(curTime);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Watch.Stop();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN class=cb3&gt;"Action {0} did consume {1:F2} s"&lt;/SPAN&gt;,&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; action, Watch.ElapsedMilliseconds / 1000.0f);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;BR&gt;The&amp;nbsp;test functions are executed in the Tests function describing itself during their execution:&lt;BR&gt;&lt;/P&gt;
&lt;DIV class=cf&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;void&lt;/SPAN&gt; Tests() 
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Stopper&lt;/SPAN&gt;.CheckTime(&lt;SPAN class=cb3&gt;"Format with String.Format"&lt;/SPAN&gt;, FormatWithStringFormat);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;Stopper&lt;/SPAN&gt;.CheckTime(&lt;SPAN class=cb3&gt;"Format with StringBuilder.AppendFormat"&lt;/SPAN&gt;, FormatWithStringBuilder);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;A possible solution to the String format question is the good old String.Format:&lt;BR&gt;
&lt;DIV class=cf&gt;&amp;nbsp; 
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;string&lt;/SPAN&gt; FormatWithStringFormat(&lt;SPAN class=cb2&gt;DateTime&lt;/SPAN&gt; time)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;return&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;String&lt;/SPAN&gt;.Format(&lt;SPAN class=cb3&gt;"{0:D2}:{1:D2}:{2:D2}.{3:D3}"&lt;/SPAN&gt;, &lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; time.Hour, time.Minute, time.Second, time.Millisecond);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;Or is StringBuilder the best one?&lt;/P&gt;
&lt;P class=cl&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb2&gt;StringBuilder&lt;/SPAN&gt; sb = &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;StringBuilder&lt;/SPAN&gt;();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;string&lt;/SPAN&gt; FormatWithStringBuilder(&lt;SPAN class=cb2&gt;DateTime&lt;/SPAN&gt; time)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sb.Length = 0; &lt;SPAN class=cb4&gt;// reset string builder buffer&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sb.AppendFormat(&lt;SPAN class=cb3&gt;"{0:D2}:{1:D2}:{2:D2}.{3:D3}"&lt;/SPAN&gt;, &lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; time.Hour, time.Minute, time.Second, time.Millisecond);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;return&lt;/SPAN&gt; sb.ToString();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;Static inits do not count and are not part of the quiz. I will post the code of the fastest(?) string time formatting function in a few days. Perhaps somebody out there can beat my function.&amp;nbsp;This are the results at my home PC (P4 3.0 GHz 1 GB RAM) after doing 2 million string formats. As you can see the String.Format is not the best option to choose ... &lt;/P&gt;
&lt;DIV style="TEXT-ALIGN: center"&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;
&lt;TABLE style="MARGIN-LEFT: 270px; WIDTH: 500px; MARGIN-RIGHT: auto; HEIGHT: 128px; TEXT-ALIGN: left" cellSpacing=2 cellPadding=2 border=1&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD style="FONT-WEIGHT: bold; WIDTH: 144px; TEXT-ALIGN: center"&gt;Formatting Algo&lt;/TD&gt;
&lt;TD style="FONT-WEIGHT: bold; WIDTH: 117px; TEXT-ALIGN: center"&gt;Elapsed Time in s&lt;/TD&gt;
&lt;TD style="FONT-WEIGHT: bold; WIDTH: 150px"&gt;Specialities&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;String.Format&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;5,54 &lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;StringBuilder.AppendFormat&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;4,92&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;Bruce ToString&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;3,36&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;Bruce FastFormat&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;1,81&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;My Best Function So Far&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;1,75&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;Unsafe Code&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;FastFormat4 by &lt;A href="http://geekswithblogs.net/gyoung/archive/2006/04/24/76187.aspx"&gt;Greg Young&lt;/A&gt;&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;1,15&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;Unsafe Code&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;NewFastestFormat&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;1,08&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;Unsafe Code&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;DansFormat&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;0,83&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;Unsafe Code&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;FormatFastSafe by Jeffrey&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;0,53&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;FastFormat6 by &lt;A href="http://blogs.extremeoptimization.com/jeffrey/archive/2006/04/26/13824.aspx"&gt;Jeffrey Sax&lt;/A&gt;&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;0,42&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;Unsafe Code&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;NewFastestStringFormatting&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;0,43&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;Unsafe Code&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="WIDTH: 144px"&gt;FormatFast7 by&amp;nbsp;&lt;A href="http://geekswithblogs.net/gyoung/archive/2006/04/24/76187.aspx"&gt;Greg Young&lt;/A&gt;&lt;/TD&gt;
&lt;TD style="WIDTH: 117px; TEXT-ALIGN: center"&gt;0,33&lt;/TD&gt;
&lt;TD style="WIDTH: 150px"&gt;Unsafe Code, Breakes the CLR string handling!&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR&gt;Bruce did submit some samples which perform already very good. His FastFormat is looks&amp;nbsp;very promising. By realizing that you can use a fixed char array and also knowing that the string constructor can take an char array we can get an impressive performance boost by a factor three compared to the most obvious solution. But the very creative solution from &lt;A href="http://geekswithblogs.net/gyoung/archive/2006/04/24/76187.aspx"&gt;Greg Young&lt;/A&gt; did really surprise me. He did: &lt;BR&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Realize that the DateTime get properties do not simply return an int but do some calculations on a 64 bit value. Repeated access to the get properties do cost therefore quite a lot and can be minimized by doing it yourself and cache the calculated values for hour, minute, second and millisecond.&lt;/LI&gt;
&lt;LI&gt;Use unsafe access to the array (My Best Function does this already but the effect is quite small).&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The result is a&amp;nbsp; 40% speed improvement. Very impressive Greg. There proves once again that there is always somebody around who is smarter than you. But all is not all lost. I did even optimize Gregs function a little bit and made it more readable. I strongly believe that reflector had something to do with his success. Here is the code for my NewFastFormat&amp;nbsp;function which is by the way much more comprehensible:&lt;BR&gt;&lt;/P&gt;
&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;public&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;unsafe&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;string&lt;/SPAN&gt; NewFastestFormat(&lt;SPAN class=cb2&gt;DateTime&lt;/SPAN&gt; time)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;fixed&lt;/SPAN&gt; (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;* charP = &amp;amp;FixedCharArray[0]) &lt;SPAN class=cb3&gt;// Get rid of bounds checking&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; hour = time.Hour; &lt;SPAN class=cb3&gt;// Cache property values&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; minute = time.Minute;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; second = time.Second;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; ms = time.Millisecond;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// hour&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[0] = (&lt;SPAN class=cb2&gt;Char&lt;/SPAN&gt;)(&lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt; + hour / 10);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[1] = (&lt;SPAN class=cb2&gt;Char&lt;/SPAN&gt;)(&lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt; + hour % 10);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// minute&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[3] = (&lt;SPAN class=cb2&gt;Char&lt;/SPAN&gt;)(&lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt; + minute / 10);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[4] = (&lt;SPAN class=cb2&gt;Char&lt;/SPAN&gt;)(&lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt; + minute % 10);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// seconds&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[6] = (&lt;SPAN class=cb2&gt;Char&lt;/SPAN&gt;)(&lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt; + second / 10);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[7] = (&lt;SPAN class=cb2&gt;Char&lt;/SPAN&gt;)(&lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt; + second % 10);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// miliseconds&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[9] = (&lt;SPAN class=cb2&gt;Char&lt;/SPAN&gt;)(&lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt; + ms / 100);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[10] = (&lt;SPAN class=cb2&gt;Char&lt;/SPAN&gt;)(&lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt; + ms % 100 / 10);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[11] = (&lt;SPAN class=cb2&gt;Char&lt;/SPAN&gt;)(&lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt; + ms % 10);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;return&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;String&lt;/SPAN&gt;(FixedCharArray);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;BR&gt;My last trick I came up with is that I do not need to recreate the array at every call. And that I can initialize the "&lt;SPAN style="FONT-WEIGHT: bold; TEXT-DECORATION: underline"&gt;: : .&lt;/SPAN&gt;" characters only once in the ctor of my class. This squeezes the last(?) bit of performance out of it. I can&amp;nbsp;only say: WOW. I did not think that this big improvement was possible. After some test runs with and without&amp;nbsp;unsafe array access it turned out I was not able to measure any difference!&amp;nbsp;Strange&amp;nbsp;but true which proves that you should not&amp;nbsp;trust other peoples claims about performance. I am surprised that nobody did try it with a generic List&amp;lt;Char&amp;gt;. Many say that generics are faster but the string constructor does not take such a thing. So we have to convert it back to a char array which kills&amp;nbsp;performance. Another interesting thing is that the StringBuilder index operator [] is significantly slower than the char array indexer.&amp;nbsp;Now we can safely say that we have here&amp;nbsp;the fastest&amp;nbsp;time to string formatting function known to C# programmers until somebody finds an even faster solution.&lt;BR&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Update&lt;/SPAN&gt;: There is somebody out there who did find an even faster solution: &lt;A href="http://blogs.extremeoptimization.com/jeffrey/archive/2006/04/26/13824.aspx"&gt;Jeffrey Sax&lt;/A&gt; is obviously a real number crack and plays dirty tricks by creeping around expensive divisions by&amp;nbsp;turning them into multiplications with a following bit shift. I had numerical maths at university during my studies of nuclear physics but this is really tough stuff. To push my ego up I must say that he does this for a living at &lt;A href="http://www.extremeoptimization.com/Default.aspx"&gt;Extreme Optimizations&lt;/A&gt;, which explains why he did find such a dam good function. He did make it by a factor 2.6 faster that our previously fastest function! This means that we posses now a string formatting function which is over 14 times faster than our original String.Format. Unbelievable. Now I think we have reached a limit where we have used all our tricks that are imaginable. I do not expect that there is room for another big improvement such as this. But one never knows.... As usual I did a little reformat and added some comments to make it easier for normal people to get the idea:&lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;unsafe&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;string&lt;/SPAN&gt; NewFastestStringFormatting(&lt;SPAN class=cb2&gt;DateTime&lt;/SPAN&gt; time)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;fixed&lt;/SPAN&gt; (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;* charP = &amp;amp;FixedCharArray[0])&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// Calculate values by getting the ms values first and do then &lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// shave off the hour minute and second values with multiplications&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// and bit shifts instead of simple but expensive divisions.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;long&lt;/SPAN&gt; ticks = time.Ticks;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; ms = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)((ticks / 10000) % 86400000); &lt;SPAN class=cb3&gt;// Get daytime in ms which does fit into an int&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; hour = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)(&lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(ms &amp;gt;&amp;gt; 7, 9773437) &amp;gt;&amp;gt; 38); &lt;SPAN class=cb3&gt;// well ... it works&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ms -= 3600000 * hour;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; minute = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)((&lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(ms &amp;gt;&amp;gt; 5, 2290650)) &amp;gt;&amp;gt; 32);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ms -= 60000 * minute;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; second = ((ms &amp;gt;&amp;gt; 3) * 67109) &amp;gt;&amp;gt; 23;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ms -= 1000 * second;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// Hour&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; temp = (hour * 13) &amp;gt;&amp;gt; 7;&amp;nbsp; &lt;SPAN class=cb3&gt;// 13/128 is nearly the same as /10 for values up to 65&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[0] = (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(temp + &lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[1] = (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(hour - 10 * temp + &lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt;); &lt;SPAN class=cb3&gt;// Do subtract to get remainder instead of doing % 10&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// Minute&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp = (minute * 13) &amp;gt;&amp;gt; 7;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// 13/128 is nearly the same as /10 for values up to 65&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[3] = (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(temp + &lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[4] = (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(minute - 10 * temp + &lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt;); &lt;SPAN class=cb3&gt;// Do subtract to get remainder instead of doing % 10&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// Second&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp = (second * 13) &amp;gt;&amp;gt; 7; &lt;SPAN class=cb3&gt;// 13/128 is nearly the same as /10 for values up to 65&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[6] = (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(temp + &lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[7] = (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(second - 10 * temp + &lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// Milisecond&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp = (ms * 41) &amp;gt;&amp;gt; 12;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb3&gt;// 41/4096 is nearly the same as /100 &lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[9] = (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(temp + &lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ms -= 100 * temp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp = (ms * 205) &amp;gt;&amp;gt; 11;&amp;nbsp; &lt;SPAN class=cb3&gt;// 205/2048 is nearly the same as /10&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[10] = (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(temp + &lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ms -= 10 * temp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; charP[11] = (&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(ms + &lt;SPAN class=cb4&gt;'0'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;return&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;new&lt;/SPAN&gt; &lt;SPAN class=cb2&gt;String&lt;/SPAN&gt;(FixedCharArray);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;As you can see this is not exactly rocket science it is worse than that. You have to be very careful to not get biten by a rounding error due to a too big value. The initial version of Jeffrey did have a small error which caused for some ms values strange characters. Really great work Jeffrey. For educational purposes I do also post there the fastest format function which does no unsafe tricks. Credits for FormatFastSafe go&amp;nbsp;also to Jeffrey. If &amp;nbsp;still more solutions are required to be considered I am forced to rate the solutions in categories. Fastest function ever is Gregs solution but you would not use this function really since it does break the CLR promise that string objects are immutable. The fastest function which does contain unsafe code and&amp;nbsp;works correctly was sent by Jeffry. And the fastest (correct working) function which uses no unsafe tricks was also sent by Jeffrey. The fastest safe function is this one:&lt;BR&gt;&lt;BR&gt;
&lt;STYLE type=text/css&gt;
.cf { font-family: Courier New; font-size: 10pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: teal; }
.cb3 { color: maroon; }
&lt;/STYLE&gt;
&lt;/P&gt;
&lt;DIV class=cf&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;private&lt;/SPAN&gt; &lt;SPAN class=cb1&gt;string&lt;/SPAN&gt; FormatFastSafe(&lt;SPAN class=cb2&gt;DateTime&lt;/SPAN&gt; time)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;long&lt;/SPAN&gt; ticks = time.Ticks;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; n1 = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)(ticks &amp;gt;&amp;gt; 32);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; n2 = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)ticks;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;if&lt;/SPAN&gt; (n2 &amp;lt; 0)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n1++;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ticks = (((&lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(429497, n2) - (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)(&lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(1161359156, n2) &amp;gt;&amp;gt; 32) - &lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(1161359156, n1)) &amp;gt;&amp;gt; 32))&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; + &lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(n1, 429497);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n1 = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)(ticks &amp;gt;&amp;gt; 32);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n2 = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)ticks;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;if&lt;/SPAN&gt; (n2 &amp;lt; 0)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n1++;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; q = n1 * 50 + ((50 * (n2 &amp;gt;&amp;gt; 16)) &amp;gt;&amp;gt; 16) - (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)(System.&lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(1244382467, n1) &amp;gt;&amp;gt; 32) - 1;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; r = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)(ticks - System.&lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(q, 86400000));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;if&lt;/SPAN&gt; (r &amp;gt; 86400000)&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; r -= 86400000;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;int&lt;/SPAN&gt; unit = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)(&lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(r &amp;gt;&amp;gt; 7, 9773437) &amp;gt;&amp;gt; 32) &amp;gt;&amp;gt; 6;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n2 = (unit * 13) &amp;gt;&amp;gt; 7;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n1 = r - 3600000 * unit;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FixedCharArray[0] = ((&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(n2 + &lt;SPAN class=cb3&gt;'0'&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FixedCharArray[1] = ((&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(unit - 10 * n2 + &lt;SPAN class=cb3&gt;'0'&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; unit = (&lt;SPAN class=cb1&gt;int&lt;/SPAN&gt;)((&lt;SPAN class=cb2&gt;Math&lt;/SPAN&gt;.BigMul(n1 &amp;gt;&amp;gt; 5, 2290650)) &amp;gt;&amp;gt; 32);&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n1 -= 60000 * unit;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n2 = (unit * 13) &amp;gt;&amp;gt; 7;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FixedCharArray[3] = ((&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(n2 + &lt;SPAN class=cb3&gt;'0'&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FixedCharArray[4] = ((&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(unit - 10 * n2 + &lt;SPAN class=cb3&gt;'0'&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; unit = ((n1 &amp;gt;&amp;gt; 3) * 67109) &amp;gt;&amp;gt; 23;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n1 -= 1000 * unit;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n2 = (unit * 13) &amp;gt;&amp;gt; 7;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FixedCharArray[6] = ((&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(n2 + &lt;SPAN class=cb3&gt;'0'&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FixedCharArray[7] = ((&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(unit - 10 * n2 + &lt;SPAN class=cb3&gt;'0'&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n2 = (n1 * 41) &amp;gt;&amp;gt; 12;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FixedCharArray[9] = ((&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(n2 + &lt;SPAN class=cb3&gt;'0'&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n1 -= 100 * n2;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; n2 = (n1 * 205) &amp;gt;&amp;gt; 11;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FixedCharArray[10] = ((&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(n2 + &lt;SPAN class=cb3&gt;'0'&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FixedCharArray[11] = ((&lt;SPAN class=cb1&gt;char&lt;/SPAN&gt;)(n1 - 10 * n2 + &lt;SPAN class=cb3&gt;'0'&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class=cb1&gt;return&lt;/SPAN&gt; FixedCharArray.ToString();&lt;/P&gt;
&lt;P class=cl&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P class=cl&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;This contest was really fun and I did not expect to get so many good solutions. Thanks to everyone who did submit sample code. The code presented here is a very good example of&amp;nbsp;highly optimized C# code which (I guess) cannot be found anywhere else on the net. I hope it does help other programmers to see where they burn CPU cycles and by what means you can solve a performance problem. &lt;/P&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=76146"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=76146" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/76146.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2006/04/23/76146.aspx</guid>
            <pubDate>Sun, 23 Apr 2006 22:55:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/76146.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2006/04/23/76146.aspx#feedback</comments>
            <slash:comments>23</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/76146.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/76146.aspx</trackback:ping>
        </item>
        <item>
            <title>Assembly.LoadFrom demystified</title>
            <link>http://geekswithblogs.net/akraus1/archive/2006/04/04/74323.aspx</link>
            <description>&lt;!--StartFragment --&gt;&amp;nbsp;Rules are meant to be bro.. ahem expanded. Especially if everybody tells you not to do so ;-).&amp;nbsp;The &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconhowruntimelocatesassemblies.asp"&gt;Assembly Loader Rules&lt;/A&gt; are for example a very strict set of rules. One rule of the loader is that you cannot load assemblies from directories above the directory where the executable is located. This can cause some headaches if you want to share some assemblies across applications which are not installed inside the GAC. You can bypass this restriction by using &lt;A href="http://msdn2.microsoft.com/en-US/library/1009fa28%28VS.80%29.aspx"&gt;Assembly.LoadFrom&lt;/A&gt;. A very good article about the how and when of &lt;A href="http://msdn2.microsoft.com/en-US/library/1009fa28%28VS.80%29.aspx"&gt;Assembly.LoadFrom&lt;/A&gt; can be found at &lt;A href="http://www.gotdotnet.com/team/clr/LoadFromIsolation.aspx"&gt;GotDotNet&lt;/A&gt;. If you have to deal with plugin architectures and loosely coupled components you will soon end up in a situation where you need to expand the default assembly loader rules. You can configure the loader to some extent via your App.Config file but you cannot configure a &lt;A href="http://msdn2.microsoft.com/en-us/library/823z9h8w%28VS.80%29.aspx"&gt;probing path&lt;/A&gt; for directories above your application binary location. Lets suppose you have an application lets call it App.exe which references ClassLibrary1.Dll &amp;nbsp;which in turn references ClassLibrary2.dll. Now you have to call Assembly.LoadFrom for ClassLibrary1.dll for some reason (e.g. it is a configured plugin). I have drawn the dependencies in the diagram below&lt;BR&gt;&lt;BR&gt;
&lt;DIV style="TEXT-ALIGN: center"&gt;&lt;IMG style="WIDTH: 421px; HEIGHT: 329px" alt="UML Model" src="/images/geekswithblogs_net/akraus1/3938/o_AssemblyLoadFromUML.JPG"&gt;&lt;BR&gt;&lt;/DIV&gt;&lt;BR&gt;
&lt;H2&gt;&lt;A href="http://geekswithblogs.net/akraus1/articles/74319.aspx"&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Show More ....&lt;/SPAN&gt;&lt;/A&gt;&lt;/H2&gt;&lt;BR&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=74323"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=74323" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/74323.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2006/04/04/74323.aspx</guid>
            <pubDate>Wed, 05 Apr 2006 02:39:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/74323.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2006/04/04/74323.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/74323.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/74323.aspx</trackback:ping>
        </item>
        <item>
            <title>Call Properties through delegates and pass them by Reference</title>
            <link>http://geekswithblogs.net/akraus1/archive/2006/02/10/69047.aspx</link>
            <description>&amp;nbsp;After reading &lt;A href="http://blogs.msdn.com/jmstall/archive/2006/02/09/property_by_Ref.aspx#comments"&gt;Mike Stalls Debugging Blog&lt;/A&gt; I was wondering if it is really impossible to pass property values by reference:&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void PassIntByRef(ref int x)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PassIntByRef(ref MyProperty);&lt;/FONT&gt;&amp;nbsp; &amp;lt;-- this is illegal in C#.&lt;BR&gt;Another restriction which is often annoying is that you cannot have property delegates.&lt;BR&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white 0% 50%; COLOR: black; FONT-FAMILY: Courier New; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;PropertyDelegation&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;delegate&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;ReturnValueDelegate&lt;/SPAN&gt;();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: teal"&gt;ReturnValueDelegate&lt;/SPAN&gt; ValueGetter;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; x = 0;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; X &amp;nbsp;// This is our beloved property we care so much about.&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; x; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt; { x = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; PropertyDelegation()&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ValueGetter = X; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Illegal in C#&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ValueGetter = GetX; &amp;nbsp; // Legal code&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white 0% 50%; COLOR: black; FONT-FAMILY: Courier New; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; GetX()&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; X;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp; &amp;nbsp;}&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;The above&amp;nbsp;workaround with the GetX function is not really nice but it works and I&amp;nbsp;wondered if there is no other way to do this. After 10 minutes thinking about this I did realize that both problems could be solved by a reference type which provides the needed functionality.&lt;BR&gt;
&lt;UL&gt;
&lt;LI&gt;The new type should provide a getter and setter for the wrapped property. 
&lt;LI&gt;Expose Get and Set functions to attach delegates to it 
&lt;LI&gt;Provide a as much type safety as possible.&lt;/LI&gt;&lt;/UL&gt;Lets look what the C# compiler does produce for a Property with a getter and setter. Using Reflector we quickly see what really happens:&lt;BR&gt;
&lt;DIV style="TEXT-ALIGN: center"&gt;&lt;IMG title="Property with Getter and Setter under the Reflector Microscope" style="WIDTH: 159px; HEIGHT: 50px" alt="Property with Getter and Setter under the Reflector Microscope" src="/images/geekswithblogs_net/akraus1/3938/o_Reflector_Property.JPG"&gt;&lt;BR&gt;&lt;/DIV&gt;Hm the property is in reality a set of two methods. If we get our hands on both methods we can create a delegate from it and call them by our selfs inside our wrapper class (reference type object). Extensions to compiled types at run time calls for the reflection API. In our case&amp;nbsp;the PropertyInfo class provides all information we want. The result of this is my new class PropertyInvoker which fulfills all three requirements.&lt;BR&gt;&lt;BR&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white 0% 50%; COLOR: black; FONT-FAMILY: Courier New; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;PropertyInvoker&lt;/SPAN&gt;&lt;T&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;PropertyInfo&lt;/SPAN&gt; propInfo;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; obj;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; PropertyInvoker(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; PropertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; o)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; obj = o;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; propInfo = o.GetType().GetProperty(PropertyName);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; T Property&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; (T) propInfo.GetValue(obj, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; propInfo.SetValue(obj, &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; T GetProperty()&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; Property;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetProperty(T value)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Property = value;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;This little helper allows us to create a delegate function from a property:&lt;BR&gt;&lt;BR&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white 0% 50%; COLOR: black; FONT-FAMILY: Courier New; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;delegate&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;ReturnValueDelegate&lt;/SPAN&gt;();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: teal"&gt;ReturnValueDelegate&lt;/SPAN&gt; ValueGetter;&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white 0% 50%; COLOR: black; FONT-FAMILY: Courier New; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Create delegate around the get property of X&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ValueGetter = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;ReturnValueDelegate&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;PropertyInvoker&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;(&lt;SPAN style="COLOR: maroon"&gt;"X"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;).GetProperty);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; realval = ValueGetter.Invoke(); &lt;SPAN style="COLOR: green"&gt;// Call get property with our new invoker&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;Properties cannot be really passed by reference but at least we did come quite close to our initial goal:&lt;BR&gt;&lt;BR&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white 0% 50%; COLOR: black; FONT-FAMILY: Courier New; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Add(&lt;SPAN style="COLOR: teal"&gt;PropertyInvoker&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt; value)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; value.Property++;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white 0% 50%; COLOR: black; FONT-FAMILY: Courier New; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Pass property by reference inside wrapped type&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Add(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;PropertyInvoker&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;(&lt;SPAN style="COLOR: maroon"&gt;"X"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;/P&gt;&lt;/DIV&gt;Ok this looks nice but how about performance? Reflection is known to be slow after all. The good news is that on my P4 3.0 GHz PC at home I can call value.Property++ over 300 000 times/s. This is quite good for an indirect function call via reflection. The bad news is the native property is much better. To be precise about several hundred times faster. Why? I think the JITer is very good at detecting this set_X and get_X methods in my code and does simply inline the calls. Against inlined function calls my PropertyInvoker has clearly no chance since at runtime there is no method call&amp;nbsp;due to god work of the JITer. To check this assumption and to prevent such an unfair "optimization"I decorated the getter and setter of my property with the [&lt;SPAN style="COLOR: teal"&gt;MethodImpl&lt;/SPAN&gt;(&lt;SPAN style="COLOR: teal"&gt;MethodImplOptions&lt;/SPAN&gt;.NoInlining)] attribute to prevent inlining. After that I got "only" a performance penalty of about&amp;nbsp;200 times slower than the native method call. To sum it up: My method is much slower as a native property (or wrapped function call) but still far away from being slow in absolute terms. If &amp;nbsp;this little class does help you to write nicer and cleaner code give it a try.&lt;BR&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white 0% 50%; COLOR: black; FONT-FAMILY: Courier New; moz-background-clip: initial; moz-background-origin: initial; moz-background-inline-policy: initial"&gt;&lt;/DIV&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=69047"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=69047" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/akraus1/aggbug/69047.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Alois Kraus</dc:creator>
            <guid>http://geekswithblogs.net/akraus1/archive/2006/02/10/69047.aspx</guid>
            <pubDate>Sat, 11 Feb 2006 02:39:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/akraus1/comments/69047.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/akraus1/archive/2006/02/10/69047.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/akraus1/comments/commentRss/69047.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/akraus1/services/trackbacks/69047.aspx</trackback:ping>
        </item>
    </channel>
</rss>