﻿<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" 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:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:betag="http://dotnetblogengine.net/schemas/tags">
  <channel>
    <title>Laserbrain — a software blog</title>
    <description>From the heart and brain of a C# developer</description>
    <link>http://laserbrain.se/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.NET 3.1.1.0</generator>
    <language>en-GB</language>
    <blogChannel:blogRoll>http://laserbrain.se/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>Mattias Larsson</dc:creator>
    <dc:title>Laserbrain — a software blog</dc:title>
    <geo:lat>5,536.000000</geo:lat>
    <geo:long>13.000000</geo:long>
    <item>
      <title>Async singleton initialization</title>
      <description>&lt;p&gt;&lt;span lang="EN-US"&gt;Still in these days you once in a while have use for a singleton object. But how do you achieve a thread-safe singleton that requires async initialization?&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;First, let's start off with a good ol' singleton class that can be constructed
synchronously.&lt;/span&gt;&lt;/p&gt;&lt;h3&gt;&lt;span style="line-height: 1.42857;"&gt;Synchronous Singleton&lt;/span&gt;&lt;/h3&gt;&lt;p class="MsoNormal" style="margin: 7.5pt 0cm;"&gt;Assuming your singleton class needs some
initialization data that it can construct itself (synchronously). To make it
thread-safe, we rely on locking on a static read-only object.&lt;/p&gt;&lt;pre&gt;&lt;span lang="EN-US" style="color: blue; "&gt;public&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;class&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;MySingleton
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;{&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;static&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;readonly&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;object&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; SyncObj = &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;new&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;object&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;();&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;static&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;MySingleton&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; _singleton;&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;summary&amp;gt;
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; The public available singleton instance.
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;a name="OLE_LINK2"&gt;&lt;/a&gt;&lt;a name="OLE_LINK1"&gt;&lt;span lang="EN-US" style="color: blue; "&gt;public&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;static&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;MySingleton&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; Singleton
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;get
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&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&gt;&lt;span lang="EN-US" style="color: blue; "&gt;if &lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;(_singleton == &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;null&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&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; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;lock&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; (SyncObj)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;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; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;if &lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;(_singleton == &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;null&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&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; _singleton = CreateSingleton();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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&gt;&lt;span lang="EN-US" style="color: blue; "&gt;return &lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;_singleton;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/a&gt;&lt;span lang="EN-US" style="color: black; "&gt;&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;summary&amp;gt;
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; The private instance-constructor, taking some data.&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;MySingleton&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;(&lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;SomeData&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; someData)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SomeData = someData;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; Some data that the singleton exposes.
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;public&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;SomeData&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; SomeData&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;get&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;; }&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; Create the singleton instance.&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;static&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;MySingleton&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; CreateSingleton()&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;SomeData&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; someData = CreateSomeData();&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;return&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;new&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;MySingleton&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;(someData);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;summary&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; A private method that can create some data, called once.&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;///&lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: gray; "&gt;&amp;lt;/summary&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;static&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;SomeData&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; CreateSomeData()
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt;// ...some way to create some data...
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
}&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;p class="Code"&gt;&lt;span lang="EN-US"&gt;In the version above the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Singleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;property is designed
to work as quick as possible, only entering the &lt;span style="font-weight: bold;"&gt;lock &lt;/span&gt;if there is a chance that
the class hasn’t yet been initialized. Within the lock a method that creates the&amp;nbsp;&lt;span style="font-weight: bold;"&gt;MySingleton &lt;/span&gt;object&amp;nbsp;is called. This way it's ensured that only one&amp;nbsp;instance of the &lt;span style="font-weight: bold;"&gt;MySingleton &lt;/span&gt;is ever created.&lt;/span&gt;&lt;/p&gt;&lt;h3 style="margin: 7.5pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Asynchronous Singleton&lt;/span&gt;&lt;/h3&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;But what to do if the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;CreateSomeData&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;method would work asynchronous, returning a &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Task&amp;lt;SomeData&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt; instead, using the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;await&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;keyword? Let's modify the code in this way:&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style=""&gt; &lt;span style="color:blue"&gt;static &lt;/span&gt;&lt;span style="color:blue"&gt;async&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#2B91AF"&gt;SomeData&lt;/span&gt;&amp;gt; CreateSomeData()&lt;br&gt;{
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green"&gt;// ...some way to create some data, using the await keyword...
&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style=""&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;p class="Code"&gt;&lt;span lang="EN-US"&gt;Making this change, we should play nice and propagate the &lt;span style="font-weight: bold;"&gt;async&lt;/span&gt;’ness
to the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;CreateSingleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;method:&lt;span style="font-family: Helvetica, sans-serif; font-size: 10.5pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style=""&gt; &lt;span style="color:blue"&gt;static &lt;/span&gt;&lt;span style="color:blue"&gt;async&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#2B91AF"&gt;MySingleton&lt;/span&gt;&amp;gt; CreateSingleton()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2B91AF"&gt;SomeData &lt;/span&gt;someData = &lt;span style="color:blue"&gt;await&lt;/span&gt; CreateSomeData();
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue"&gt;return&lt;/span&gt; &lt;span style="color:blue"&gt;new&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;MySingleton&lt;/span&gt;(someData);
}&lt;/span&gt;&lt;span lang="EN-US" style=""&gt;&lt;br&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;That would naturally imply that the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Singleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;property should also be asynchronous, returning a &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Task&amp;lt;MySingleton&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt; instead.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;So, is that possibly?&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;First of all, it turns out that &lt;span style="font-style: italic;"&gt;it’s not allowed &lt;/span&gt;to
use the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;async&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;keyword with a property. But this is no biggie, we could afford
a method instead.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;But more importantly, we are &lt;span style="font-style: italic;"&gt;not allowed &lt;/span&gt;to use a &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;lock&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;around an &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;await&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;keyword. (This is actually a good thing, since the &lt;span style="font-weight: bold;"&gt;lock &lt;/span&gt;would really work
against us here – so we should thank the C# team for not allowing this!)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;So how do we do this then? As a starter, a naïve,
non thread-safe solution would look like this:&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;&lt;span lang="EN-US" style="color: blue; "&gt;public&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;static&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;Task&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;lt;&lt;/span&gt;&lt;span lang="EN-US" style="color: rgb(43, 145, 175); "&gt;MySingleton&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;&amp;gt; Singleton&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
{&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;get&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; { &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;return &lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;CreateSingleton(); } &lt;/span&gt;&lt;span lang="EN-US" style="color: green; "&gt;// WOHAA! NOT SINGLETON AT ALL!
&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;Requesting the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Singleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;property would of course lead to a call to the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;CreateSingleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt; method every time, and would hardly be a singleton...&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;But the solution to make it both a singleton and
thread-safe turns out to be ridiculously simple – we let the inner mechanisms
of the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Task&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;class work for us!&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;So, how &lt;span style="font-style: italic;"&gt;does
&lt;/span&gt;a task work?&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;Let’s say you have an instance of a &lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;span lang="EN-US"&gt;Task&lt;/span&gt;&lt;span lang="EN-US"&gt;&amp;lt;&lt;/span&gt;&lt;span lang="EN-US"&gt;T&lt;/span&gt;&lt;span lang="EN-US"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt; and you &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;await&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;it once. Now the
task is executed, and a value of &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;T&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt; is produced and
returned to you. Now what if you &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;await&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;the &lt;span style="font-style: italic;"&gt;same &lt;/span&gt;task instance &lt;span style="font-style: italic;"&gt;again&lt;/span&gt;? In this case the task just returns the previously produced
value immediately in a completely synchronous manner.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;And what if you &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;await&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;the same task instance simultaneously from multiple threads (where you
would normally get a race condition)? Well, the first one (since there &lt;span style="font-style: italic;"&gt;will &lt;/span&gt;be one that gets there first) will execute
the task code while the others will wait for the result to be processed. Then when
the result has been produced, all the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;await&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;’s will finish (virtually) simultaneously and return the value.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;So, a &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Task&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;&lt;span style="font-style: italic;"&gt;is &lt;/span&gt;thread-safe, and it looks as if we
could use this power in our advantage here!&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;In the end, all we need is to replace the old &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Singleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;property with this one:&lt;span style="font-family: Helvetica, sans-serif; font-size: 10.5pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;&lt;span lang="EN-US" style="color: blue; "&gt;public&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span lang="EN-US" style="color: blue; "&gt;static&lt;/span&gt;&lt;span lang="EN-US" style="color: black; "&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175); line-height: 1.42857;"&gt;Task&lt;/span&gt;&lt;span style="line-height: 1.42857; color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="line-height: 1.42857; color: rgb(43, 145, 175);"&gt;MySingleton&lt;/span&gt;&lt;span style="line-height: 1.42857; color: black;"&gt;&amp;gt; Singleton { &lt;/span&gt;&lt;span style="line-height: 1.42857; color: blue;"&gt;get&lt;/span&gt;&lt;span style="line-height: 1.42857; color: black;"&gt;; } = CreateSingleton();&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;p&gt;&lt;span&gt;Or, rewritten in a more classic C# way (without making use of the fancy new C# 6 “&lt;/span&gt;&lt;span style="line-height: 1.42857; font-style: italic;"&gt;read-only properties&lt;/span&gt;&lt;span style="line-height: 1.42857;"&gt;” and “&lt;/span&gt;&lt;span style="line-height: 1.42857; font-style: italic;"&gt;property initializers&lt;/span&gt;&lt;span style="line-height: 1.42857;"&gt;”):&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style=""&gt; &lt;span style="color:blue"&gt;static &lt;/span&gt;&lt;span style="color:blue"&gt;readonly&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#2B91AF"&gt;AsyncSingleton&lt;/span&gt;&amp;gt; CreateSingletonTask = CreateSingleton();&lt;br&gt;&lt;span style="color:blue"&gt;
public&lt;/span&gt; &lt;span style="color:blue"&gt;static&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;Task&lt;/span&gt;&amp;lt;&lt;span style="color:#2B91AF"&gt;AsyncSingleton&lt;/span&gt;&amp;gt; Singleton
{
&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; CreateSingletonTask; }
}&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;span style="font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;So the dead simple solution is to assign the &lt;span style="font-weight: bold;"&gt;async &lt;/span&gt;method
to a read-only (static) &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Task&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;field (or
read-only property in C# 6). This gives you both a singleton and thread-safety for free!&lt;/span&gt;&lt;/p&gt;&lt;h3 style="margin: 7.5pt 0cm;"&gt;&lt;span lang="EN-US"&gt;Lazy Synchronous Singleton&lt;/span&gt;&lt;/h3&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;Being empowered with the super-simple &lt;span style="font-weight: bold;"&gt;async &lt;/span&gt;version,
you might wonder if there really is no similar way to achieve this in the
synchronous version?&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;Of course there is!&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;Revisiting the synchronous first version of the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;MySingleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;in this blog post, you may replace the &lt;span style="font-weight: bold;"&gt;_&lt;/span&gt;&lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;SyncObj&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;and &lt;span style="font-weight: bold;"&gt;_&lt;/span&gt;&lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;singleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;fields and the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Singleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;property with these two lines:&lt;span style="font-family: Helvetica, sans-serif; font-size: 10.5pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style=""&gt; &lt;span style="color:blue"&gt;static &lt;/span&gt;&lt;span style="color:blue"&gt;readonly&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color:#2B91AF"&gt;MySingleton&lt;/span&gt;&amp;gt; LazySingleton&amp;nbsp;= &lt;span style="color:blue"&gt;new&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color:#2B91AF"&gt;MySingleton&lt;/span&gt;&amp;gt;(CreateSingleton);&lt;br&gt;&lt;span style="color:blue"&gt;
public&lt;/span&gt; &lt;span style="color:blue"&gt;static&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;MySingleton &lt;/span&gt;Singleton =&amp;gt; LazySingleton.Value;&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;Or, if you prefer the pre-C# 6 code:&lt;span style="font-family: Helvetica, sans-serif; font-size: 10.5pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre&gt;&lt;span lang="EN-US" style="color: blue; "&gt;private&lt;/span&gt;&lt;span lang="EN-US" style=""&gt; &lt;span style="color:blue"&gt;static &lt;/span&gt;&lt;span style="color:blue"&gt;readonly&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color:#2B91AF"&gt;MySingleton&lt;/span&gt;&amp;gt; LazySingleton &amp;nbsp;= &lt;span style="color:blue"&gt;new&lt;/span&gt; &lt;span style="color:#2B91AF"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color:#2B91AF"&gt;MySingleton&lt;/span&gt;&amp;gt;(CreateSingleton);&lt;br&gt;&lt;span style="color:blue"&gt;
public&lt;/span&gt;&lt;span style="color:black"&gt; &lt;/span&gt;&lt;span style="color:blue"&gt;static&lt;/span&gt;&lt;span style="color:black"&gt; &lt;/span&gt;&lt;span style="color:#2B91AF"&gt;MySingleton&lt;/span&gt;&lt;span style="color:black"&gt; Singleton
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue"&gt;get&lt;/span&gt;&lt;span style="color:black"&gt; { &lt;/span&gt;&lt;span style="color:blue"&gt;return&lt;/span&gt;&lt;span style="color:black"&gt; &lt;/span&gt;&lt;span style="color:#2B91AF"&gt;MySingleton&lt;/span&gt;&lt;span style="color:black"&gt;.Value; }
}&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span style="font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;Wow, magic! No need for locks or anything! So how does
this work?&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;Well, the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Lazy&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt; can be seen as the synchronous counterpart to the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Task&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt; in some aspects. At least it has the same singleton and
thread-safety properties, and keeps its results for latecomers.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;As you can see, the constructor of the &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Lazy&amp;lt;MySingleton&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US"&gt; is given the means
of producing a &lt;/span&gt;&lt;span class="CodeChar"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;MySingleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;&amp;nbsp;(i.e. the&amp;nbsp;&lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;CreateSingleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;&lt;span style="font-weight: bold;"&gt;&amp;nbsp;&lt;/span&gt;method)&amp;nbsp;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;– not an &lt;span style="font-style: italic;"&gt;instance &lt;/span&gt;of the type
directly&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;. Then, when someone is requesting the &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Singleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;property, the &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Value&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt; of the &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Lazy&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;-class is accessed. And in the same manner as the
&lt;span style="font-weight: bold;"&gt;Task&lt;/span&gt;, the first one reaching for the &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Value&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;will make the actual call to the &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;CreateSingleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;method. Any other thread asking at the same time will simply be hanging in
the &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;Value&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;getter, and continues once the &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;CreateSingleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;method is done and the &lt;span style="font-weight: bold;"&gt;Value&lt;/span&gt; is produced. Further on, any consecutive calls to the &lt;span style="font-weight: bold;"&gt;Value &lt;/span&gt;getter will return
immediately with the same instance of the &lt;/span&gt;&lt;span class="CodeChar" style="line-height: 1.42857;"&gt;&lt;span lang="EN-US" style="font-weight: bold;"&gt;MySingleton&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="line-height: 1.42857;"&gt;. So, again, the &lt;span style="font-weight: bold;"&gt;Lazy &lt;/span&gt;gives us both a singleton and thread-safety.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;

&lt;/p&gt;&lt;p&gt;&lt;span lang="EN-US"&gt;And there was much rejoicing!&lt;/span&gt;&lt;/p&gt;</description>
      <link>http://laserbrain.se/post/2015/11/22/async-singleton-initialization.aspx</link>
      <comments>http://laserbrain.se/post/2015/11/22/async-singleton-initialization.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=8957e760-1e95-4b20-bfce-ed2ea66391c7</guid>
      <pubDate>Sun, 22 Nov 2015 13:00:00 +0200</pubDate>
      <category>C#</category>
      <category>General</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=8957e760-1e95-4b20-bfce-ed2ea66391c7</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=8957e760-1e95-4b20-bfce-ed2ea66391c7</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2015/11/22/async-singleton-initialization.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=8957e760-1e95-4b20-bfce-ed2ea66391c7</wfw:commentRss>
    </item>
    <item>
      <title>Exporting a certificate without its private key and password-protect the output? Beware, there is a serious trap!</title>
      <description>&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;So you have
an instance of an &lt;span style="font-weight: bold;"&gt;X509Certificate2 &lt;/span&gt;(or
&lt;b&gt;X509Certificate&lt;/b&gt;) that you want to
export as a byte array – and you want to exclude the private key&amp;nbsp;–&amp;nbsp;&lt;span style="font-weight: bold; font-style: italic;"&gt;and &lt;/span&gt;encrypt the output using a password.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;You have
found the &lt;b&gt;Export&lt;/b&gt; method of the certificate class which takes one of the &lt;b&gt;X509ContentType&lt;/b&gt; enum
values and an optional password. The &lt;a href="https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate.export(v=vs.110).aspx" target="_blank"&gt;MSDN help&lt;/a&gt; informs that you must choose
between &lt;b&gt;X509ContentType&lt;/b&gt;.&lt;b&gt;Cert&lt;/b&gt;, &lt;b&gt;X509ContentType&lt;/b&gt;.&lt;b&gt;SerializedCert&lt;/b&gt;
and &lt;b&gt;X509ContentType&lt;/b&gt;.&lt;b&gt;Pkcs12&lt;/b&gt;&amp;nbsp;for the export to work. You also find out (by experimenting or &lt;a href="http://bfy.tw/2isn" target="_blank"&gt;googling&lt;/a&gt;) that exporting using&amp;nbsp;&lt;b&gt;X509ContentType&lt;/b&gt;.&lt;b&gt;Cert&lt;/b&gt; produces a serialized certificate &lt;span style="text-decoration: underline;"&gt;without the private key&lt;/span&gt; – just what
you want! Hooray!&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;Now you
specify a password and think you’re OK.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;&lt;span style="font-weight: bold; color: rgb(255, 0, 0);"&gt;Fail. &lt;/span&gt;You
are not. &lt;span style="font-weight: bold; font-style: italic;"&gt;The password is actually ignored.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;If you try
this:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style="margin-bottom:0cm;margin-bottom:.0001pt;line-height:
normal;mso-layout-grid-align:none;text-autospace:none"&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;byte&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;[] a = cert.Export(&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509ContentType&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;.Cert);&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;byte&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;[] b = cert.Export(&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509ContentType&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;.Cert, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;"P@ssw0rd #1"&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;);&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;byte&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;[] c = cert.Export(&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509ContentType&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;.Cert, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;"P@ssw0rd #2"&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 1.42857; font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 1.42857;"&gt;The resulting byte arrays
&lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;a&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt;, &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;b&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; and &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;c&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; will have the
&lt;span style="text-decoration: underline;"&gt;exact same content&lt;/span&gt;, even though you specified different passwords! (And it behaves the same if you use the &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;SecureString&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; class.)&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="line-height: 1.42857;"&gt;Personally
I would expect the &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;Export&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; method to throw an exception when specifying&amp;nbsp;&lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;X509ContentType&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt;.&lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;Cert&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; together with a password (other
than &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;null&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt;). That would give me, as a developer, a clear sign that I am trying to use an unsupported parameter combination which gives me a chance to try to figure out a work-around. As it is now I am lead to believe that the output content is in fact encrypted.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 1.42857;"&gt;It is also
possible to recreate the certificate again from the byte array giving any
password:&lt;/span&gt;&lt;/p&gt;&lt;pre style="margin-bottom:0cm;margin-bottom:.0001pt;line-height:
normal;mso-layout-grid-align:none;text-autospace:none"&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;var&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; certX = &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;new&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509Certificate2&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;(a, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;"P@ssw0rd #2"&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;);&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;var&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; certY = &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;new&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509Certificate2&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;(b, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;"correct horse battery staple"&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 1.42857; font-size: 8px;"&gt;&lt;br&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 1.42857;"&gt;Both &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;certX&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; and &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;certY&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt;&amp;nbsp;above will be correctly reconstructed.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="line-height: 1.42857;"&gt;Here is a
simple solution you can use to export a certificate without its private key &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;&lt;i&gt;and&lt;/i&gt;&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt;
encrypt the exported bytes:&lt;/span&gt;&lt;/p&gt;&lt;pre style="margin-bottom:0cm;margin-bottom:.0001pt;line-height:
normal;tab-stops:1.0cm 2.0cm 3.0cm 4.0cm;mso-layout-grid-align:none;text-autospace:
none"&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;byte&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;[] ExportCertificate(&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509Certificate&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; certificate, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;string&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; password, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;bool&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; includePrivateKey)&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;{&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;if&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; (!includePrivateKey)&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: green; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;// Export the certificate (temporarily) using the content type "Cert".&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: green; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;// The exported cert will NOT include the private key -- but it will not&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: green; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;// be encrypted (the given password will be ignored). So we are not there yet.&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;byte&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;[] exportedWithoutPrivateKey = certificate.Export(&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509ContentType&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;.Cert, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;""&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;);&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: green; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;// Now recreate the certificate again from the exported bytes.&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: green; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;// The recreated certificate will not contain a private key.&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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;span style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;certificate = &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;new&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; &lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509Certificate2&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;(&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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; exportedWithoutPrivateKey,&lt;br&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;""&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: green; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;// The (ignored) password&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&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; &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509KeyStorageFlags&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;.Exportable | &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509KeyStorageFlags&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;.PersistKeySet);&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;&lt;o:p&gt;&amp;nbsp;&lt;br&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: green; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;// Export the certificate using the PKCS #12 format.&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: green; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;// This content type will include the private key among the exported bytes (if there is one).&lt;br&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;return&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; certificate.Export(&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509ContentType&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;.Pkcs12, password);&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;Now calling this method, specifying two different passwords and asking &lt;span style="font-style: italic; font-weight: bold;"&gt;not &lt;/span&gt;to include the
private key...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre style="margin-bottom:0cm;margin-bottom:.0001pt;line-height:
normal;mso-layout-grid-align:none;text-autospace:none"&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;byte&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;[] d = ExportCertificate(cert, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;"P@ssw0rd #1"&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;false&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;);&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;byte&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;[] e = ExportCertificate(cert, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;"P@ssw0rd #2"&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;false&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p class="MsoNormal"&gt;&lt;br&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 1.42857;"&gt;...generates two
byte arrays &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;d&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; and &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;e&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt;&amp;nbsp;that are different. Further on, if
you try to recreate it you &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;&lt;i&gt;must&lt;/i&gt;&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; specify the correct password.&lt;/span&gt;&lt;/p&gt;&lt;pre style="margin-bottom:0cm;margin-bottom:.0001pt;line-height:
normal;mso-layout-grid-align:none;text-autospace:none"&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;var&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; certZ = &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;new&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509Certificate2&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;(d, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;"P@ssw0rd #1"&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;);&lt;br&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;var&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; fails = &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: blue; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;new&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt; &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(43, 145, 175); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;X509Certificate2&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;(d, &lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;"&lt;span style="font-size: 9.5pt;"&gt;correct horse battery staple&lt;/span&gt;&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: rgb(163, 21, 21); background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;"&lt;/span&gt;&lt;span lang="EN-US" style="font-size: 9.5pt; font-family: Consolas; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 1.42857;"&gt;&lt;br&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="line-height: 1.42857;"&gt;The &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;certZ&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; will be correctly reconstructed,
but the second&amp;nbsp;&lt;/span&gt;&lt;span style="line-height: 1.42857;"&gt;try (with wrong password) will throw
a &lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;CryptographicException&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt; with the
message “&lt;/span&gt;&lt;b style="line-height: 1.42857;"&gt;The specified network password
is not correct.&lt;/b&gt;&lt;span style="line-height: 1.42857;"&gt;”&lt;/span&gt;&lt;/p&gt;</description>
      <link>http://laserbrain.se/post/2015/11/10/exporting-a-certificate-without-its-private-key-and-password-protect-the-output-beware-there-is-a-serious-trap1.aspx</link>
      <comments>http://laserbrain.se/post/2015/11/10/exporting-a-certificate-without-its-private-key-and-password-protect-the-output-beware-there-is-a-serious-trap1.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=ad44879d-e105-47d6-9ea8-1721bb26c971</guid>
      <pubDate>Tue, 10 Nov 2015 19:00:00 +0200</pubDate>
      <category>C#</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=ad44879d-e105-47d6-9ea8-1721bb26c971</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=ad44879d-e105-47d6-9ea8-1721bb26c971</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2015/11/10/exporting-a-certificate-without-its-private-key-and-password-protect-the-output-beware-there-is-a-serious-trap1.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=ad44879d-e105-47d6-9ea8-1721bb26c971</wfw:commentRss>
    </item>
    <item>
      <title>Best practices of the Dictionary class</title>
      <description>&lt;p&gt;It’s important to know how the standard classes behave so that your code gets optimized, not performing unnecessary checks or making unnecessary calls.&lt;/p&gt;
&lt;p&gt;Here's a number of issues I've seen time after time:&lt;/p&gt;

&lt;!--  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx --&gt;
&lt;h2&gt;&lt;span style="line-height: 1.1;"&gt;&lt;font size="4"&gt;
Fetching a value from the dictionary only if it exists
&lt;/font&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;div&gt;In the first version (&lt;font color="#ff6666"&gt;&lt;b&gt;bad&lt;/b&gt;&lt;/font&gt;) there are &lt;b&gt;&lt;i&gt;two&lt;/i&gt; &lt;/b&gt;dictionary lookups performed. It’s like walk in to a public library asking the librarian whether a certain book is in – and when the librarian comes back and says “yes, it’s in”, you ask again saying "Good, could you please get it for me?". In real life it's pretty obvious that this is a silly behavior, but in the computer world it's not that apparent.&lt;br&gt;&lt;/div&gt;

&lt;table width="100%"&gt;
	&lt;tbody&gt;
		&lt;tr bgcolor="#FFAAAA"&gt;
			&lt;td&gt;&amp;nbsp;&lt;br&gt;
				&lt;pre class="brush: xml"&gt;if (myDict.ContainsKey(key)) // First lookup.
{
	ValueType value = myDict[key]; // Second lookup.
	// ... use the "value" here ...
}
				&lt;/pre&gt;
			&lt;/td&gt;
			&lt;td width="5%"&gt;&amp;nbsp; &lt;b&gt;:-(&lt;/b&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;

&lt;div&gt;In the second version&amp;nbsp;(&lt;font color="#33cc00"&gt;&lt;b&gt;good&lt;/b&gt;&lt;/font&gt;), only &lt;i&gt;&lt;b&gt;one&lt;/b&gt;&lt;/i&gt; lookup is performed:&lt;br&gt;&lt;/div&gt;

&lt;table width="100%"&gt;
	&lt;tbody&gt;
		&lt;tr bgcolor="#AAFFAA"&gt;
			&lt;td&gt;&amp;nbsp;&lt;br&gt;
				&lt;pre class="brush: xml"&gt;ValueType value;
if (myDict.TryGetValue(key, out value))
{
	// ... use the "value" here ...
}
				&lt;/pre&gt;
			&lt;/td&gt;
			&lt;td width="5%"&gt;&amp;nbsp; &lt;b&gt;:-)&lt;/b&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;!--  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx --&gt;

&lt;br&gt;

&lt;!--  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx --&gt;
&lt;h2&gt;&lt;span style="line-height: 1.1;"&gt;&lt;font size="4"&gt;
Accessing all the keys and values
&lt;/font&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;div&gt;I've seen many real-life cases where access to both the key and value is needed in a foreach-loop, and the iteration is performed over the Keys-properties while the value is accessed by a lookup:&lt;br&gt;&lt;/div&gt;

&lt;table width="100%"&gt;
	&lt;tbody&gt;
		&lt;tr bgcolor="#FFAAAA"&gt;
			&lt;td&gt;&amp;nbsp;&lt;br&gt;
				&lt;pre class="brush: xml"&gt;foreach (TKey key in myDict.Keys)
{
	TValue value = myDict[key];
	// ... use the "key" and "value" here ...
}
				&lt;/pre&gt;
			&lt;/td&gt;
			&lt;td width="5%"&gt;&amp;nbsp; &lt;b&gt;:-(&lt;/b&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;

&lt;div&gt;If you want&amp;nbsp;&lt;i&gt;&lt;b&gt;both&lt;/b&gt;&amp;nbsp;&lt;/i&gt;the key&amp;nbsp;and&lt;b&gt;&amp;nbsp;&lt;/b&gt;the value simultaneously it’s better to iterate over the dictionary as &lt;b&gt;KeyValuePair&lt;/b&gt;s:&lt;br&gt;&lt;/div&gt;

&lt;table width="100%"&gt;
	&lt;tbody&gt;
		&lt;tr bgcolor="#AAFFAA"&gt;
			&lt;td&gt;&amp;nbsp;&lt;br&gt;
				&lt;pre class="brush: xml"&gt;foreach (KeyValuePair&amp;lt;TKey, TValue&amp;gt; keyValuePair in myDict)
{
	TKey key = keyValuePair.Key;
	TValue value = keyValuePair.Value;
	// ... use the "key" and "value" here ...
}
				&lt;/pre&gt;
			&lt;/td&gt;
			&lt;td width="5%"&gt;&amp;nbsp; &lt;b&gt;:-)&lt;/b&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;

&lt;div&gt;(Obviously; if you want&amp;nbsp;&lt;i&gt;only&amp;nbsp;&lt;/i&gt;the keys you should use the&amp;nbsp;&lt;b&gt;Keys&lt;/b&gt;-property, and if you want&amp;nbsp;&lt;i&gt;only&amp;nbsp;&lt;/i&gt;the values use the&amp;nbsp;&lt;b&gt;Values&lt;/b&gt;-property)&lt;br&gt;&lt;/div&gt;

&lt;!--  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx --&gt;

&lt;br&gt;

&lt;!--  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx --&gt;
&lt;h2&gt;&lt;span style="line-height: 1.1;"&gt;&lt;font size="4"&gt;
Overwriting a value
&lt;/font&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;table width="100%"&gt;
	&lt;tbody&gt;
		&lt;tr bgcolor="#FFAAAA"&gt;
			&lt;td&gt;&amp;nbsp;&lt;br&gt;
				&lt;pre class="brush: xml"&gt;if (myDict.ContainsKey(key)) // First lookup.
{
	myDict.Remove(key); // Second lookup.
}
myDict.Add(key, value); // Third lookup.
				&lt;/pre&gt;
			&lt;/td&gt;
			&lt;td width="5%"&gt;&amp;nbsp; &lt;b&gt;:-(&lt;/b&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;

&lt;div&gt;There is really no need to remove a key before assigning a new value to it:&lt;br&gt;&lt;/div&gt;

&lt;table width="100%"&gt;
	&lt;tbody&gt;
		&lt;tr bgcolor="#AAFFAA"&gt;
			&lt;td&gt;&amp;nbsp;&lt;br&gt;
				&lt;pre class="brush: xml"&gt;myDict[key] = value;
				&lt;/pre&gt;&lt;/td&gt;
			&lt;td width="5%"&gt;&amp;nbsp; &lt;b&gt;:-)&lt;/b&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;!--  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx --&gt;

&lt;br&gt;

&lt;!--  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx --&gt;
&lt;h2&gt;&lt;span style="line-height: 1.1;"&gt;&lt;font size="4"&gt;
Removing a value
&lt;/font&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;div&gt;Once again, the following implementation leads to two lookups:&lt;br&gt;&lt;/div&gt;

&lt;table width="100%"&gt;
	&lt;tbody&gt;
		&lt;tr bgcolor="#FFAAAA"&gt;
			&lt;td&gt;&amp;nbsp;&lt;br&gt;
				&lt;pre class="brush: xml"&gt;bool entryExists = myDict.ContainsKey(key); // First lookup.
if (entryExists)
{
	myDict.Remove(key); // Second lookup.
}
return entryExists;
				&lt;/pre&gt;
			&lt;/td&gt;
			&lt;td width="5%"&gt;&amp;nbsp; &lt;b&gt;:-(&lt;/b&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;


&lt;div&gt;The Remove-method is actually very kind. It doesn't crash if you try to remove something that is not there but returns a bool telling the result of the operation:&lt;br&gt;&lt;/div&gt;

&lt;table width="100%"&gt;
	&lt;tbody&gt;
		&lt;tr bgcolor="#AAFFAA"&gt;
			&lt;td&gt;&amp;nbsp;&lt;br&gt;
				&lt;pre class="brush: xml"&gt;return myDict.Remove(key);
				&lt;/pre&gt;
			&lt;/td&gt;
			&lt;td width="5%"&gt;&amp;nbsp; &lt;b&gt;:-)&lt;/b&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;!--  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx --&gt;

&lt;br&gt;

&lt;!--  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx --&gt;
&lt;h2&gt;&lt;span style="line-height: 1.1;"&gt;&lt;font size="5"&gt;
When may my call fail?
&lt;/font&gt;&lt;/span&gt;&lt;/h2&gt;

&lt;table width="100%"&gt;
	&lt;tbody&gt;
		&lt;tr&gt;
			&lt;td width="38%"&gt;&lt;font size="4"&gt;The call...&lt;/font&gt;&lt;/td&gt;
			&lt;td width="62%"&gt;&lt;font size="4"&gt;...fails when&lt;/font&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td bgcolor="#AAFFAA"&gt;&lt;pre class="brush: xml"&gt;myDict[key] = value;&lt;/pre&gt;&lt;/td&gt;
			&lt;td bgcolor="#AAFFAA"&gt;Never&amp;nbsp;&lt;i&gt;(*)&lt;/i&gt;&lt;br&gt;The value will be&amp;nbsp;&lt;i&gt;added&lt;/i&gt;&amp;nbsp;if it's not already in the dictionary, and it will be&amp;nbsp;&lt;i&gt;replaced&lt;/i&gt;&amp;nbsp;if it does.&lt;/td&gt;
		&lt;/tr&gt;

		&lt;tr&gt;&lt;td&gt;&lt;font size="1"&gt;&amp;nbsp;&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;

		&lt;tr&gt;
			&lt;td bgcolor="#FFAAAA"&gt;&lt;pre class="brush: xml"&gt;var value = myDict[key];&lt;/pre&gt;&lt;/td&gt;
			&lt;td bgcolor="#FFAAAA"&gt;When the entry “key” does not exist.&lt;/td&gt;
		&lt;/tr&gt;

		&lt;tr&gt;&lt;td&gt;&lt;font size="1"&gt;&amp;nbsp;&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;

		&lt;tr&gt;
			&lt;td bgcolor="#FFAAAA"&gt;&lt;pre class="brush: xml"&gt;myDict.Add(key, value);&lt;/pre&gt;&lt;/td&gt;
			&lt;td bgcolor="#FFAAAA"&gt;When the entry “key” already exists.&lt;/td&gt;
		&lt;/tr&gt;

		&lt;tr&gt;&lt;td&gt;&lt;font size="1"&gt;&amp;nbsp;&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;

		&lt;tr&gt;
			&lt;td bgcolor="#AAFFAA"&gt;&lt;pre class="brush: xml"&gt;myDict.TryGetValue(key, out value);&lt;/pre&gt;&lt;/td&gt;
			&lt;td bgcolor="#AAFFAA"&gt;Never&amp;nbsp;&lt;i&gt;(*)&lt;/i&gt;&lt;/td&gt;
		&lt;/tr&gt;

		&lt;tr&gt;&lt;td&gt;&lt;font size="1"&gt;&amp;nbsp;&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;

		&lt;tr&gt;
			&lt;td bgcolor="#AAFFAA"&gt;&lt;pre class="brush: xml"&gt;myDict.Remove(key);&lt;/pre&gt;&lt;/td&gt;
			&lt;td bgcolor="#AAFFAA"&gt;Never&amp;nbsp;&lt;i&gt;(*)&lt;/i&gt;&lt;br&gt;(And the call returns false if the entry “key” does not exist.)&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;

&lt;div&gt;&lt;i&gt;(*)&lt;/i&gt;&amp;nbsp;Well, you get an ArgumentNullException if the key is null...&lt;br&gt;&lt;/div&gt;

&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;</description>
      <link>http://laserbrain.se/post/2014/02/09/best-practices-of-the-dictionary-class.aspx</link>
      <comments>http://laserbrain.se/post/2014/02/09/best-practices-of-the-dictionary-class.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=125040a6-36f3-4d93-814e-c6afcaa975c7</guid>
      <pubDate>Sun, 09 Feb 2014 15:50:00 +0200</pubDate>
      <category>C#</category>
      <category>General</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=125040a6-36f3-4d93-814e-c6afcaa975c7</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=125040a6-36f3-4d93-814e-c6afcaa975c7</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2014/02/09/best-practices-of-the-dictionary-class.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=125040a6-36f3-4d93-814e-c6afcaa975c7</wfw:commentRss>
    </item>
    <item>
      <title>Fixing the assembly hopping between WP7 and WP8</title>
      <description>&lt;p&gt;If you want to develop an Windows Phone app that can run on both WP7 and WP8 you can simply submit one built for Windows Phone 7. But if you want to take advantage of some of the new feature of Windows Phone 8 you will need to submit two versions of it (one for 7 and one for 8). This means that you will have to maintain two copies of the project file -- but you obviously don't want two copies of each source code file.&lt;/p&gt;
&lt;p&gt;This can be solved by putting the code in a &lt;a title="MSDN: Cross-Platform Development with the .NET Framework" href="http://msdn.microsoft.com/en-us/library/gg597391.aspx" target="_blank"&gt;&lt;strong&gt;Portable Class Library&lt;/strong&gt;&lt;/a&gt; and then reference it by the two projects. There are however some limitations using a Portable Class Library, and you might find yourself stuck between a rock and a hard [coded] place trying to use it.&lt;/p&gt;
&lt;p&gt;Each time I was facing the dual project problem, I ended up with using linked source code files instead, i.e. making one of the projects the "main" project, and then simply &lt;strong&gt;linking&lt;/strong&gt;&amp;nbsp;all the source code files into the other project (i.e. "Add As Link" rather than "Add" when adding "Existing Item..."). An annoying problem with &lt;em&gt;this&lt;/em&gt; solution is however that each time you &lt;em&gt;rename&lt;/em&gt; a source file you will need to remove and add the linked file again. &amp;nbsp;Aaarrgghh! &amp;nbsp;:[ &amp;nbsp;But for me this actually was less painful than using the Portable Class Library.&lt;/p&gt;
&lt;p&gt;Anyway, this way you will only have to maintain one copy of the code files that are identical on both platforms. For the ones that differs a bit, you either use compiler switches (e.g. "#if WP7 [...] #endif") -- or totally different classes (i.e. non-linked code, if there are "too much" differences to make use of #if's).&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;strong&gt;But when it comes to XAML you are in more trouble.&lt;/strong&gt;&lt;/span&gt; For instance, if you are sharing the same implementation of a page in both WP7 and WP8, you will notice that the home of the &lt;strong&gt;Pivot&lt;/strong&gt; (and &lt;strong&gt;Panorama&lt;/strong&gt;) has changed between the two platforms. &lt;em&gt;Ouch.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;WP7&lt;/strong&gt; your page declaration looks like this:&lt;/p&gt;
&lt;pre class="brush: xml"&gt;xmlns:xxx="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"&lt;/pre&gt;
&lt;p&gt;but your &lt;strong&gt;WP8&lt;/strong&gt;&amp;nbsp;projects want &lt;em&gt;this&lt;/em&gt; line:&lt;/p&gt;
&lt;pre class="brush: xml"&gt;xmlns:xxx="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"&lt;/pre&gt;
&lt;p&gt;As you can see above, the &lt;strong&gt;assembly name&lt;/strong&gt; has changed.&lt;/p&gt;
&lt;p&gt;This makes you yearn for the following (illegal) construction:&lt;/p&gt;
&lt;pre class="brush: csharp"&gt;&amp;lt;phone:PhoneApplicationPage&amp;nbsp;
&amp;nbsp; &amp;nbsp; x:Class="Wp7App.PivotPage1"
&amp;nbsp; &amp;nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
&amp;nbsp; &amp;nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
#if WP7
    xmlns:xxx="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
#elif WP8
    xmlns:xxx="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
#endif
&amp;nbsp; &amp;nbsp; xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
&amp;nbsp; &amp;nbsp; xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&lt;/pre&gt;
&lt;p&gt;But unfortunately compiler switches are not allowed in XAML... So the above will NOT compile. :(&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;There is however a pretty simple solution to this. Create the following two classes:&lt;/p&gt;
&lt;pre class="brush: csharp"&gt;namespace MyShared
{
	using Microsoft.Phone.Controls;

	public class MyPivot : Pivot
	{
	}

	public class MyPivotItem : PivotItem
	{
	}
}&lt;/pre&gt;
&lt;p&gt;This file can be linked across the two platforms intact without any need of #if's (since it wasn't the &lt;em&gt;namespace&lt;/em&gt; that was changed, "just" the &lt;em&gt;assembly&lt;/em&gt;).&lt;/p&gt;
&lt;p&gt;Now you simply use the &lt;strong&gt;MyPivot&amp;nbsp;&lt;/strong&gt;wrapper classes (instead of the Pivot directly) in your XAML, and you're good to go:&lt;/p&gt;
&lt;pre class="brush: csharp"&gt;&amp;lt;Phone:PhoneApplicationPage 
    x:Class="MyShared.PivotPage1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:MyShared="clr-namespace:MyShared"
    xmlns:Phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"&amp;gt;

    &amp;lt;Grid&amp;gt;
        &amp;lt;MyShared:MyPivot Title="MY APPLICATION"&amp;gt;
            &amp;lt;MyShared:MyPivotItem Header="item1"&amp;gt;
                &amp;lt;Grid/&amp;gt;
            &amp;lt;/MyShared:MyPivotItem&amp;gt;
            &amp;lt;MyShared:MyPivotItem Header="item2"&amp;gt;
                &amp;lt;Grid/&amp;gt;
            &amp;lt;/MyShared:MyPivotItem&amp;gt;
        &amp;lt;/MyShared:MyPivot&amp;gt;
    &amp;lt;/Grid&amp;gt;
    
&amp;lt;/Phone:PhoneApplicationPage&amp;gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description>
      <link>http://laserbrain.se/post/2013/04/28/Fixing-the-assembly-hopping-between-WP7-and-WP8.aspx</link>
      <comments>http://laserbrain.se/post/2013/04/28/Fixing-the-assembly-hopping-between-WP7-and-WP8.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=66e5fa29-03f3-4c26-a7ca-37b936aa6340</guid>
      <pubDate>Sun, 28 Apr 2013 16:47:00 +0200</pubDate>
      <category>C#</category>
      <category>WP7</category>
      <category>WP8</category>
      <category>XAML</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=66e5fa29-03f3-4c26-a7ca-37b936aa6340</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=66e5fa29-03f3-4c26-a7ca-37b936aa6340</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2013/04/28/Fixing-the-assembly-hopping-between-WP7-and-WP8.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=66e5fa29-03f3-4c26-a7ca-37b936aa6340</wfw:commentRss>
    </item>
    <item>
      <title>Running a "Hello World" written in C# on a Raspberry Pi for Linux n00bs like me</title>
      <description>&lt;p&gt;A while ago I found myself in the possession of a &lt;a title="www.raspberrypi.org" href="http://www.raspberrypi.org/" target="_blank"&gt;Raspberry Pi&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;O the joy of a new toy! But what to do with it?&lt;/p&gt;
&lt;p&gt;Well, being a C# developer of course I would like to be able to install and run my own applications on it &amp;mdash; and I wanted to do that remotely from my regular laptop.&lt;/p&gt;
&lt;p&gt;The biggest problem was that I have no Linux skills what so ever. I've been living a comfortable (well, hm... you know...) Windows life ever since the days of the Amiga and Commodore 64.&amp;nbsp;But how hard could it be?&lt;/p&gt;
&lt;p&gt;The following are the steps I had to go through to make it work. It's a hodgepodge of&amp;nbsp;wisdoms found all over the&amp;nbsp;internet. So; &lt;strong&gt;disclaimer&lt;/strong&gt;: It worked for me, but I can't promise it will work for you!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here we go...&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 1. Formatting the SD card&lt;/h2&gt;
&lt;p&gt;To do this properly you'll need to use the &lt;a title="https://www.sdcard.org/downloads/formatter_3/" href="https://www.sdcard.org/downloads/formatter_3/" target="_blank"&gt;SD Formatter 3.1&lt;/a&gt; application.&lt;/p&gt;
&lt;p&gt;Install it and launch it having your SD card inserted. Simply press the Format button. It's enough to just do a quick format.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://laserbrain.se/image.axd?picture=2012%2f11%2fraspberrypi_1.png" alt="" /&gt;&lt;/p&gt;
&lt;h3&gt;Important note!&lt;/h3&gt;
&lt;p&gt;If you are repeating this step later on, your SD card might appear to have lost a few gigabytes, only showing something like 56MB! This is due to that Windows cannot see the Linux partitions, and the 56MB is just some left-overs that Linux didn't claim.&lt;/p&gt;
&lt;p&gt;You have at least a couple of options to reclaim the full space in Windows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Find yourself a [video]camera or any other non-windows device and try to format it in there. I first tried my age-old compact camera, but it just said that the card was bad. Then I tried my somewhat newer video camera, and it formatted it nicely and I got back my gigabytes. Phew...&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Use the dreaded (?)&amp;nbsp;&lt;a title="http://dansraspberrypi.tumblr.com/post/26482663944/how-to-re-format-a-linux-sd-card-in-windows" href="http://dansraspberrypi.tumblr.com/post/26482663944/how-to-re-format-a-linux-sd-card-in-windows" target="_blank"&gt;Flashnul (blog post)&lt;/a&gt; tool. I haven't tried this one since my first option worked for me. Be careful...&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 2. Installing a Linux distribution on the freshly formatted SD card&lt;/h2&gt;
&lt;p&gt;I used &lt;a title="www.raspberrypi.org/downloads" href="http://www.raspberrypi.org/downloads" target="_blank"&gt;the Raspbian &amp;ldquo;wheezy&amp;rdquo; image&lt;/a&gt;. (I was somehow drawn to its tag-line "If you&amp;rsquo;re just starting out, this is the image we recommend you use".)&lt;/p&gt;
&lt;p&gt;To write the image to the card you'll need the &lt;a title="https://launchpad.net/win32-image-writer/+download" href="https://launchpad.net/win32-image-writer/+download" target="_blank"&gt;Image Writer for Windows&lt;/a&gt; application. There is no installation required; just download and launch the Win32DiskImager.exe. Launch it, click the blue little folder-button and point out the image file. Then press the Write button to start writing. This will take a few moments, depending on the size and speed of your SD card.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://laserbrain.se/image.axd?picture=2012%2f11%2fraspberrypi_2.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 3. First launch&lt;/h2&gt;
&lt;p&gt;After preparing the SD-card it's time for the first launch.&lt;/p&gt;
&lt;p&gt;Insert the SD card into the Pi and plug in an ethernet cable (with internet access), a USB keyboard and a monitor/tv (hdmi). Finally plug in the micro USB power adapter to boot it up. You will see a whole lot of text burst out on the screen. You do not need to read it all. :)&lt;/p&gt;
&lt;p&gt;After a while a classic ASCII-artish gray popup appears, entitled Raspi-config.&lt;/p&gt;
&lt;p&gt;Here you can for instance set your keyboard layout. When you're done, use the tab key to move to the Finish button, then press enter to access the console.&lt;/p&gt;
&lt;p&gt;This popup is shown only the very first launch time. The following times you start the Pi you will need to enter a user name and password to access the console. The default user name is "pi" and the password is "raspberry".&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 4. Optional: Setting the keyboard locale&lt;/h2&gt;
&lt;p&gt;If you didn't change the keyboard layout in that first popup (which only will show up upon the first launch), you can do it using the following command:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;sudo nano /etc/default/keyboard &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Where 'sudo' means 'run this command as super user', 'nano' is a text editor and '/etc/default/keyboard' is the file to edit.&lt;/p&gt;
&lt;p&gt;Now the text editor appears, and you may change the 'XKBLAYOUT' to whatever you like. I chose 'se' for Sweden.&lt;/p&gt;
&lt;p&gt;Save with &lt;strong&gt;Ctrl + O&lt;/strong&gt; (!) and exit with &lt;strong&gt;Ctrl + X&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Reboot. This can be done by pulling the power cord or by issuing the command&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;sudo reboot &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 5. Set a fixed IP-address&lt;/h2&gt;
&lt;p&gt;Since you want to remotely connect to your Raspberry Pi you might want to give it a fixed IP-address. This step is of course optional.&lt;/p&gt;
&lt;p&gt;If you want to know the current network settings on your Pi, execute the following command:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;ifconfig &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;(No, that is not a typo. It should be an 'f', not a 'p'...) You will see the current IP-address under the section 'eth0', on the line starting with 'inet'. (The MAC address is found after the word 'HWaddr'.)&lt;/p&gt;
&lt;p&gt;Edit the network settings by executing:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;sudo nano /etc/network/interfaces &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;When the editor appears, replace the line&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;iface eth0 inet dhcp &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;with these lines (the #-mark means that the line is commented out.)&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;#iface eth0 inet dhcp&lt;br /&gt; iface eth0 inet static&lt;br /&gt; address 192.168.1.200&lt;br /&gt; netmask 255.255.255.0&lt;br /&gt; network 192.168.1.0&lt;br /&gt; broadcast 192.168.1.255&lt;br /&gt; gateway 192.168.1.1&lt;br /&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Obviously you will need to fill in &lt;em&gt;your&lt;/em&gt; preferred numbers here...&lt;/p&gt;
&lt;p&gt;After saving the file you'll need to reboot. The best thing is of course to check your new settings by executing the ifconfig-command again, after rebooting.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 6. Ensure the SSH service is started at boot time&lt;/h2&gt;
&lt;p&gt;This is necessary for connecting remotely. It was however already active on the image I used, but I'll keep this step here anyway.&lt;/p&gt;
&lt;p&gt;The command to execute is&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;sudo mv /boot/boot_enable_ssh.rc /boot/boot.rc &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;which means to rename/move the file/directory specified. I got the following error message:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;mv: cannot stat `/boot/boot_enable_ssh.rc': No such file or directory&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;which I interpreted that the SSH service was already active for me.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 7. Connect remotely from your favorite Windows PC&lt;/h2&gt;
&lt;p&gt;Download &lt;a title="www.chiark.greenend.org.uk/~sgtatham/putty/download.html" href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html" target="_blank"&gt;PuTTY&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There's no installation here, just an exe-file. Launch it and fill in the IP-address to your Pi (and the port, which defaults to 22) and connect with the &lt;strong&gt;Open&lt;/strong&gt; button.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://laserbrain.se/image.axd?picture=2012%2f11%2fraspberrypi_3a.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;You will get a warning of a potential security breach the first time you connect, because your PuTTY instance (obviously) does not recognize your Pi.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://laserbrain.se/image.axd?picture=2012%2f11%2fraspberrypi_3b.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;I guess it is quite harmless to press the &lt;strong&gt;Yes&lt;/strong&gt; button here.&lt;/p&gt;
&lt;p&gt;Now the Pi console window greets you by asking for your username (pi) and password (raspberry). You're in!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 8. FTP&lt;/h2&gt;
&lt;p&gt;In addition to execute command on your Pi you will also need to transfer files to and from it. Use your favorite FTP client, for instance &lt;a title="http://filezilla-project.org/download.php" href="http://filezilla-project.org/download.php" target="_blank"&gt;FileZilla&lt;/a&gt;. Simply connect to the correct IP address, port 22, user "pi" and password "raspberry".&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 9. Install the C# compiler and .net exe-executer&lt;/h2&gt;
&lt;p&gt;To be able to compile and execute C#-code on the Raspberry Pi, you will need to install &lt;a title="www.mono-project.com" href="http://www.mono-project.com/Main_Page" target="_blank"&gt;Mono&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This turns out to be pretty simple. Just execute the following command to install the &lt;a title="www.mono-project.com/CSharp_Compiler" href="http://www.mono-project.com/CSharp_Compiler" target="_blank"&gt;dmcs (4.0 mscorlib) C# compiler&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;sudo apt-get install mono-dmcs &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This will take a while to process, the files requested will be downloaded from the internet and installed by some magic Linux fairy. Please note that in the beginning you will get a "Do you want to continue"-question. After saying &lt;strong&gt;Y&lt;/strong&gt; + enter you might want to get a quick cup of coffee (or a beer since this is most probably done off-business hours). It will take a couple of minutes.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://laserbrain.se/image.axd?picture=2012%2f11%2fraspberrypi_4.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Step 10. Compile and run your program.&lt;/h2&gt;
&lt;p&gt;Now, I wanted the full monty and even compile the C# code on the raspberry. So, in order to make it as simple as possible, I put all the code into one file (HelloPi.cs):&lt;/p&gt;
&lt;pre class="brush: csharp"&gt;namespace Test
{
	using System;

	public class HelloPi
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Hello World!");
			Console.ReadKey();
			Console.WriteLine("End.");
		}
	}
}
&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using the FTP client, transfer your 'HelloPi.cs' source file to a suitable folder. Compile the source file with the dmcs-command:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;dmcs HelloPi.cs &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Since it's not a very large application, it will be compiled pretty much instantly. Launch it with the mono-command:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: 'courier new', courier; color: #ff00ff;"&gt;mono MyApp.exe &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://laserbrain.se/image.axd?picture=2012%2f11%2fraspberrypi_5.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #ff0000;"&gt;&lt;strong&gt;Joy to the world, my app has launched!&amp;nbsp;&lt;img title="Laughing" src="http://laserbrain.se/editors/tiny_mce_3_4_3_1/plugins/emotions/img/smiley-laughing.gif" alt="Laughing" border="0" /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;Important note!&lt;/h3&gt;
&lt;p&gt;You might as well do the compilation on your PC (which is Much. More. Convenient.) and just transfer the exe-file to the Pi instead.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Some useful links&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;An A-Z Index of the &lt;a title="http://ss64.com/bash/" href="http://ss64.com/bash/" target="_blank"&gt;Bash command line&lt;/a&gt; for Linux.&lt;/li&gt;
&lt;li&gt;&lt;a title="http://www.penguintutor.com/linux/" href="http://www.penguintutor.com/linux/" target="_blank"&gt;Penguin Tutor&lt;/a&gt; - Some Linux Tutorials.&lt;/li&gt;
&lt;li&gt;Accessing the &lt;a title="http://www.raspberrypi.org/archives/tag/gpio" href="http://www.raspberrypi.org/archives/tag/gpio" target="_blank"&gt;GPIO&lt;/a&gt; on the Pi board &lt;a title="https://github.com/cypherkey/RaspberryPi.Net" href="https://github.com/cypherkey/RaspberryPi.Net" target="_blank"&gt;using C#&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a title="http://themagpi.com/" href="http://themagpi.com/" target="_blank"&gt;The MagPi&lt;/a&gt; - A Magazine for Raspberry Pi Users.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Good luck!&amp;nbsp;&lt;img title="Wink" src="http://laserbrain.se/editors/tiny_mce_3_4_3_1/plugins/emotions/img/smiley-wink.gif" alt="Wink" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description>
      <link>http://laserbrain.se/post/2012/11/21/running-a-hello-world-written-in-csharp-on-a-raspberry-pi-for-linux-n00bs-like-me.aspx</link>
      <comments>http://laserbrain.se/post/2012/11/21/running-a-hello-world-written-in-csharp-on-a-raspberry-pi-for-linux-n00bs-like-me.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=97957579-2e84-4c63-9b10-a3f2b32fd06b</guid>
      <pubDate>Wed, 21 Nov 2012 20:57:00 +0200</pubDate>
      <category>C#</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=97957579-2e84-4c63-9b10-a3f2b32fd06b</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=97957579-2e84-4c63-9b10-a3f2b32fd06b</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2012/11/21/running-a-hello-world-written-in-csharp-on-a-raspberry-pi-for-linux-n00bs-like-me.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=97957579-2e84-4c63-9b10-a3f2b32fd06b</wfw:commentRss>
    </item>
    <item>
      <title>Announcing the Diversify app for Øredev 2012</title>
      <description>&lt;p&gt;A collegue of mine, &lt;a title="Fredrik's blog" href="http://softwareblog.alcedo.com/" target="_blank"&gt;Fredrik M&amp;ouml;rk&lt;/a&gt;, noticed the announcement of the&amp;nbsp;&lt;a href="http://oredev.org/2012/mobile" target="_blank"&gt;&amp;Oslash;redev 2012 mobile app contest&lt;/a&gt;, and soon a team was formed that happily started hacking away (hosting the code in a Mercurial repository on &lt;a title="BitBucket" href="https://bitbucket.org/" target="_blank"&gt;BitBucket&lt;/a&gt;, and also using &lt;a title="Trello" href="https://trello.com/" target="_blank"&gt;Trello&lt;/a&gt; to have some sort of control of who was what, and what needed to be done). The team consisted of &lt;a title="Twitter" href="https://twitter.com/micaelcarlstedt" target="_blank"&gt;Micael Carlstedt&lt;/a&gt;, &lt;a title="Twitter" href="https://twitter.com/niclascarlstedt" target="_blank"&gt;Niclas Carlstedt&lt;/a&gt;, &lt;a title="Twitter" href="https://twitter.com/fmork" target="_blank"&gt;Fredrik M&amp;ouml;rk&lt;/a&gt;, &lt;a title="Twitter" href="https://twitter.com/markuswallen" target="_blank"&gt;Markus Wall&amp;eacute;n&lt;/a&gt;, &lt;a title="Twitter" href="https://twitter.com/sebbe_bebbe" target="_blank"&gt;Sebastian Johansson&lt;/a&gt; and myself.&lt;/p&gt;
&lt;p&gt;The app is supposed to function as a companion before and during the conference. The main functionality is that it offers you to easily navigate and explore in the conference program, and build your own personalized conference program by &amp;ldquo;favoriting&amp;rdquo; sessions. It also features a twitter feed, listening to stuff related to the conference and the app. One focus of the design has been to allow you to explore. From almost every view, there is a way to move on and get information about related stuff. One example is when you are looking at a session, from this view you can in one touch navigate to&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a page highlighting the room in which the session is on a map&lt;/li&gt;
&lt;li&gt;a page showing all sessions in that room during the conference&lt;/li&gt;
&lt;li&gt;a page showing details about the speaker (OK, this typically requires two interactions; scroll to the bottom of the page and then tap the speaker)&lt;/li&gt;
&lt;li&gt;a page showing details about each other session running in other rooms at the same time&lt;/li&gt;
&lt;li&gt;a page showing all sessions for a topic that this session also has&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This may be the most extreme example, but it&amp;rsquo;s not unique in its concept.&lt;/p&gt;
&lt;p&gt;One other thing that the app does is that it invites to sharing information about the conference. You can (again with very few interactions) share info about a session or speaker to social networks.&lt;/p&gt;
&lt;p&gt;There is an update submitted to the market place which will add a couple of features (tell the app what days you will attend and it will filter the data throughout the app accordingly and, as a personal touch, the app authors&amp;rsquo; suggestions for some sessions we find extra interesting). If you happen to have an unlocked developer phone, &lt;a title="&amp;Oslash;redev 2012 V1.1 XAP" href="http://j.mp/Pmu8Ct" target="_blank"&gt;you can grab the XAP file for that update here&lt;/a&gt;. Otherwise, go ahead and &lt;a title="The &amp;Oslash;redev 2012 app at Windows Phone Marketplace" href="http://www.windowsphone.com/s?appid=9c592fcc-17be-4115-8625-0ad0ceb2e3bf" target="_blank"&gt;get the app from the Windows Phone Marketplace&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;(Thank you Fredrik, for allowing me to steal your words from &lt;a title="Fredrik M&amp;ouml;rk's blog post" href="http://softwareblog.alcedo.com/post/2012/10/27/Announcing-the-Diversify-app-for-c398redev-2012.aspx" target="_blank"&gt;your blog post&lt;/a&gt;.&amp;nbsp;&lt;img title="Smile" src="http://laserbrain.se/editors/tiny_mce_3_4_3_1/plugins/emotions/img/smiley-smile.gif" alt="Smile" border="0" /&gt;)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Some screen shots (click for full size):&lt;br /&gt;&lt;a title="Splash screen" href="http://laserbrain.se/images/diversify.oredev.2012.wp7/1-splashscreen.png" target="_blank"&gt;&lt;img src="/images/diversify.oredev.2012.wp7/mini/1-splashscreen.png" alt="Splash screen" /&gt;&lt;/a&gt; &lt;a title="My &amp;Oslash;redev" href="http://laserbrain.se/images/diversify.oredev.2012.wp7/2-my-oredev.png" target="_blank"&gt;&lt;img src="/images/diversify.oredev.2012.wp7/mini/2-my-oredev.png" alt="My &amp;Oslash;redev" /&gt;&lt;/a&gt; &lt;a title="List of sessions" href="http://laserbrain.se/images/diversify.oredev.2012.wp7/3-list-of-sessions.png" target="_blank"&gt;&lt;img src="/images/diversify.oredev.2012.wp7/mini/3-list-of-sessions.png" alt="List of sessions" /&gt;&lt;/a&gt; &lt;a title="Session details" href="http://laserbrain.se/images/diversify.oredev.2012.wp7/4-session-detail.png" target="_blank"&gt;&lt;img src="/images/diversify.oredev.2012.wp7/mini/4-session-detail.png" alt="Session details" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a title="Speaker details" href="http://laserbrain.se/images/diversify.oredev.2012.wp7/5-speaker-detail.png" target="_blank"&gt;&lt;img src="/images/diversify.oredev.2012.wp7/mini/5-speaker-detail.png" alt="Speaker details" /&gt;&lt;/a&gt; &lt;a title="Twitter feed" href="http://laserbrain.se/images/diversify.oredev.2012.wp7/6-twitter-feed.png" target="_blank"&gt;&lt;img src="/images/diversify.oredev.2012.wp7/mini/6-twitter-feed.png" alt="Twitter feed" /&gt;&lt;/a&gt; &lt;a title="Tweet detail" href="http://laserbrain.se/images/diversify.oredev.2012.wp7/7-tweet-detail.png" target="_blank"&gt;&lt;img src="/images/diversify.oredev.2012.wp7/mini/7-tweet-detail.png" alt="Tweet detail" /&gt;&lt;/a&gt; &lt;a title="About us" href="http://laserbrain.se/images/diversify.oredev.2012.wp7/8-about-us.png" target="_blank"&gt;&lt;img src="/images/diversify.oredev.2012.wp7/mini/8-about-us.png" alt="About us" /&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://laserbrain.se/post/2012/10/31/announcing-the-diversify-app-for-oredev-2012.aspx</link>
      <comments>http://laserbrain.se/post/2012/10/31/announcing-the-diversify-app-for-oredev-2012.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=562ae839-d48b-4050-9196-625b0d7669e0</guid>
      <pubDate>Wed, 31 Oct 2012 21:02:00 +0200</pubDate>
      <category>C#</category>
      <category>WP7</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=562ae839-d48b-4050-9196-625b0d7669e0</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=562ae839-d48b-4050-9196-625b0d7669e0</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2012/10/31/announcing-the-diversify-app-for-oredev-2012.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=562ae839-d48b-4050-9196-625b0d7669e0</wfw:commentRss>
    </item>
    <item>
      <title>Rendering the audio captured by a Windows Phone device</title>
      <description>&lt;p&gt;This small app displays the audio captured by the microphone of a Windows Phone device and displays it as a continous waveform on the screen using the XNA framework.&lt;/p&gt;
&lt;p&gt;A slightly modified version of the app (changing color when touching the screen) can be found&amp;nbsp;in the &lt;a title="Marketplace" href="http://windowsphone.com/s?appid=92f1f826-54a3-4b04-b72e-73537c29cc84" target="_blank"&gt;Windows Phone Marketplace&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="\images\waveform\screenshot.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;Background&lt;/h2&gt;
&lt;p&gt;In order to capture the audio on a Windows Phone device, you need an instance to the default microphone (Microphone.Default),&amp;nbsp;decide how often you want samples using the BufferDuration-property and hook up the BufferReady-event. Then you control the capturing with the Start() and Stop() methods.&lt;/p&gt;
&lt;p&gt;The microphone is giving you samples at a fixed rate of 16 000 Hz, i.e. 16 000 samples per second. There is a property SampleRate that will tell this value. This means that you won't be able to capture audio of higher frequency than 8000 Hz (without distortion) according to &lt;a title="Wikipedia" href="http://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem" target="_blank"&gt;the sampling theorem&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You are also limited when it comes to choose the value for the BufferDuration-property; it must be between 0.1 and 1 seconds (100 - 1000 ms) in 10ms-steps. This means that you must choose a value of 100, 110, 120, ..., 990, 1000 milliseconds.&lt;/p&gt;
&lt;p&gt;When the microphone event BufferReady is fired, you should call the microphone.GetData(myBuffer)-method, in order to copy the samples from the microphone's internal buffer to a buffer that belongs to you. The recorded audio comes in the form of a byte-array, but since the samples are actually signed 16-bits integers (i.e. an integer in the range of -32'768 ... 32'767), you will probably need to do some convertion before you can process them.&lt;/p&gt;
&lt;h2&gt;How this application works&lt;/h2&gt;
&lt;p&gt;The way this application works is keeping a fixed number of narrow images, here called "(image) slices", arranged in a linked list. The images are rendered on the screen and smoothly moved from the right to the left. When the left-most slice has gone off the screen, it is moved to the far right (still outside the screen) in order to create the illusion of an unlimited number of images.&lt;/p&gt;
&lt;p&gt;&lt;img src="\images\waveform\slices.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Each slice holds the rendered samples from the content of one microphone buffer. When the buffer is filled by the microphone mechanism, the rightmost slice (outside of the screen) is rendered with these new samples and started to be moved inwards the screen.&lt;/p&gt;
&lt;p&gt;The speed of how fast the slices are moving across the screen is correlated to the duration of the buffer in such a way that the slices are moved a total of "one slice width" during the time the microphone is capturing the next buffer.&lt;/p&gt;
&lt;p&gt;Since the buffer of captured audio is rendered as graphic on a texture as soon it is received, there is no reason to keep any old buffer data. Therefore the application only keeps one buffer in memory which is reused over and over.&lt;/p&gt;
&lt;p&gt;A flag is set each time the microphone buffer is ready. Since the BufferReady event is fired on the main thread, there is no need for any lock-mechanism.&lt;/p&gt;
&lt;p&gt;In the Update()-method of the XNA app, the flag is checked whether new data has arrived, and if so, the slice in line is drawn.&amp;nbsp;In the Draw()-method, the slices are drawn on the screen and slightly moved as time goes by.&lt;/p&gt;
&lt;p&gt;The complete Visual Studio solution file&amp;nbsp;can be downloaded from&amp;nbsp;&lt;a href="\resources\waveform.zip"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here's a description of the structure of the main "Game"-class.&lt;/p&gt;
&lt;div&gt;Some constants:&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// The size of the screen.
private const int LandscapeWidth = 800;
private const int LandscapeHeight = 480;
 
// The number of milliseconds per time slice.
private const int SliceMilliseconds = 100;
&lt;/pre&gt;
&lt;div&gt;Fields regarding the microphone and the captured data:&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// The microphone and the sample data.
private readonly Microphone microphone;
private readonly byte[] microphoneData;

// The time it takes for a sample to pass over the screen.
private readonly TimeSpan screenMilliseconds = TimeSpan.FromSeconds(5);&lt;/pre&gt;
&lt;div&gt;Choose a color that is almost transparent (the last of the four parameters; it's the red, green, blue and alpha-component of the color).&amp;nbsp;The reason is that many samples are drawn on top of each other, and keeping each individual sample almost see-through makes an interesting visual effect.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// The color of the samples.
private readonly Color sampleColor = new Color(0.4f, 0.9f, 0.2f, 0.07f);
&lt;/pre&gt;
&lt;div&gt;The drawing classes. The white pixel texture is doing all the drawing.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// The sprite batch and a white single-pixel texture for drawing.
private SpriteBatch spriteBatch;
private Texture2D whitePixelTexture;&lt;/pre&gt;
&lt;div&gt;The size of each image slice.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// The size in pixels of one time slice.
private int imageSliceWidth;
private int imageSliceHeight;&lt;/pre&gt;
&lt;div&gt;There's no need to keep a reference to the linked list itself; just the first and last link. These links keeps references to their neighbors.&amp;nbsp;The currentImageSlice is the one to draw on the next time.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// The first, last and current image slices.
private LinkedListNode&amp;lt;RenderTarget2D&amp;gt; firstImageSlice;
private LinkedListNode&amp;lt;RenderTarget2D&amp;gt; lastImageSlice;
private LinkedListNode&amp;lt;RenderTarget2D&amp;gt; currentImageSlice;&lt;/pre&gt;
&lt;div&gt;The speed of the slices moving across the screen.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// The current speed of the samples.
private float pixelsPerSeconds;
&lt;/pre&gt;
&lt;div&gt;In order to know how far the current samples should be moved, the application must keep track of when they&amp;nbsp;appeared.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// The time in seconds when the current microphone data appeared.
private float microphoneDataAppearedAtSeconds;
&lt;/pre&gt;
&lt;div&gt;&amp;nbsp;The signal that&amp;nbsp;tells the Update()-method that there is new data to handle.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// A flag telling whether new data has been read.
private bool hasNewMicrophoneData;
&lt;/pre&gt;
&lt;div&gt;&amp;nbsp;The density of samples per pixel.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;// The number of samples squeezed into the width of one pixel.
private int samplesPerPixel;
&lt;/pre&gt;
&lt;div&gt;Here's the constructor. In it the graphics mode is set and the microphone is wired up&amp;nbsp;and asked to start listening.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;public Waveform()
{
    // Set the screen mode.
    new GraphicsDeviceManager(this)
    {
        PreferredBackBufferWidth = LandscapeWidth,
        PreferredBackBufferHeight = LandscapeHeight,
        IsFullScreen = true,
        SupportedOrientations =
            DisplayOrientation.Portrait |
            DisplayOrientation.LandscapeLeft |
            DisplayOrientation.LandscapeRight
    };

    // Standard setup.
    Content.RootDirectory = "Content";
    TargetElapsedTime = TimeSpan.FromTicks(333333);
    InactiveSleepTime = TimeSpan.FromSeconds(1);

    // Refer to the default microphone and hook the BufferReady-event.
    microphone = Microphone.Default;
    microphone.BufferReady += MicrophoneBufferReady;

    // Set the buffer duration to the length of one slice.
    microphone.BufferDuration = TimeSpan.FromMilliseconds(SliceMilliseconds);

    // Calculate the size in bytes of the sound buffer and create the byte array.
    var microphoneDataLength = microphone.GetSampleSizeInBytes(microphone.BufferDuration);
    microphoneData = new byte[microphoneDataLength];

    // Start listening.
    microphone.Start();
}
&lt;/pre&gt;
&lt;div&gt;In the XNA's LoadContent nothing is actually loaded since the app is not dependent on any predrawn images.&amp;nbsp;The SpriteBatch is created, the white pixel texture is generated and the image slices are initialized (as black images).&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;protected override void LoadContent()
{
    // Create a SpriteBatch for drawing.
    spriteBatch = new SpriteBatch(GraphicsDevice);

    // Create a 1x1 texture containing a white pixel.
    whitePixelTexture = new Texture2D(GraphicsDevice, 1, 1);
    var white = new[] { Color.White };
    whitePixelTexture.SetData(white);

    // Create the image slices.
    CreateSliceImages();
}
&lt;/pre&gt;
&lt;div&gt;The CreateSliceImages is calculating how many slices that are needed to cover the entire screen (plus two so there's room for movement). In the end of the method the regular RenderSamples-method is called in order to initial all the images. Since there is no data yet (all samples are zero) it will generate black images.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;private void CreateSliceImages()
{
    // Calculate how many slices that fits the screen (rounding upwards).
    var imageSlicesOnScreenCount = (int)Math.Ceiling(screenMilliseconds.TotalMilliseconds / SliceMilliseconds);

    // Calculate the width of each slice.
    imageSliceWidth = (int)Math.Ceiling((float)LandscapeWidth / imageSlicesOnScreenCount);

    // Set the height of each slice to the largest screen size
    // (this way the full height is utilized in Portrait mode without stretching)
    imageSliceHeight = LandscapeWidth;

    // Create a linked list with the required number of slices, plus two
    // so that there's room for scrolling off-screen a bit.
    var imageSlices = new LinkedList&amp;lt;RenderTarget2D&amp;gt;();
    for (var i = 0; i &amp;lt; imageSlicesOnScreenCount + 2; i++)
    {
        var imageSlice = new RenderTarget2D(GraphicsDevice, imageSliceWidth, imageSliceHeight);
        imageSlices.AddLast(imageSlice);
    }

    // Reference the first, last and current slice.
    firstImageSlice = imageSlices.First;
    lastImageSlice = imageSlices.Last;
    currentImageSlice = imageSlices.Last;

    // Calculate the speed of the pixels for an image slice.
    pixelsPerSeconds = imageSliceWidth / (SliceMilliseconds / 1000f);

    // Since the byte-array buffer really holds 16-bit samples, the actual
    // number of samples in one buffer is the number of bytes divided by two.
    var sampleCount = microphoneData.Length / 2;

    // Calculate how many samples that should be squeezed in per pixel (width).
    samplesPerPixel = (int)Math.Ceiling((float)sampleCount / imageSliceWidth);

    // Iterate through all the image slices and render with the empty microphone buffer.
    var slice = firstImageSlice;
    while (slice != null)
    {
        RenderSamples(slice.Value);
        slice = slice.Next;
    }
}
&lt;/pre&gt;
&lt;div&gt;The XNA's UnloadContent is just cleaning up what the LoadContent created.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;protected override void UnloadContent()
{
    // Dispose the SpriteBatch.
    spriteBatch.Dispose();

    // Dispose the white pixel.
    whitePixelTexture.Dispose();

    // Dispose all the image slices.
    var slice = firstImageSlice;
    while (slice != null)
    {
        slice.Value.Dispose();
        slice = slice.Next;
    }
}
&lt;/pre&gt;
&lt;div&gt;The event handler to the microphone's BufferReady-event. It copies the data from the microphone buffer and&amp;nbsp;raises the flag that new data has arrived.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;private void MicrophoneBufferReady(object sender, EventArgs e)
{
    // New microphone data can now be fetched from its buffer.

    // Copy the samples from the microphone buffer to our buffer.
    microphone.GetData(microphoneData);

    // Raise the flag that new data has come.
    hasNewMicrophoneData = true;
}
&lt;/pre&gt;
&lt;div&gt;The XNA's Update method&amp;nbsp;checks the phone's Back-button to see if it's time to quit. After that it checks the flag to see if new data has been recorded. If so, the new samples are rendered by calling&amp;nbsp;the RenderSamles-method.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;&amp;nbsp;protected override void Update(GameTime gameTime)
{
    // Exit the app if the user presses the back-button.
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
    {
        Exit();
    }

    // If new data has been captured, a new slice should be drawn.
    if (hasNewMicrophoneData)
    {
        // Reset the flag.
        hasNewMicrophoneData = false;

        // Express the current point in time as "seconds passed since start of app".
        var currentSeconds = (float)gameTime.TotalGameTime.TotalSeconds;

        // Remember the current time as "when the mic data appeared".
        microphoneDataAppearedAtSeconds = currentSeconds;

        // Render the new samples on the current image slice.
        RenderSamples(currentImageSlice.Value);

        // Select the next image slice as the new "current".
        currentImageSlice = currentImageSlice.Next &amp;#63;&amp;#63; firstImageSlice;
    }

    base.Update(gameTime);
}&lt;/pre&gt;
&lt;p&gt;The XNA's Draw-method takes care of drawing the rendered slices. It handles the two screen orientation modes; landscape and portrait, by scaling the images accordingly. If it is landscape mode the height of the images are squeezed and if it is portrait mode the width of the images are squeezed.&amp;nbsp;&lt;/p&gt;
&lt;div&gt;When all is setup, the method iterates through the images and render them one-by-one on the screen, adjusted a bit along the X-axis to make up for the time that has passed.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;protected override void Draw(GameTime gameTime)
{
    // Clear the device. (Actually unnecessary since the whole screen will be painted below.)
    GraphicsDevice.Clear(Color.Black);

    // Calculate the "screen width-scale", to allow the app to be drawn both in
    // Landscape and Portrait mode.
    // In Landscape mode, the screenWidthScale will be 1.0 (i.e. original scale)
    // In Portrait mode, the screen must be squeezed.
    var screenWidthScale = (float)GraphicsDevice.Viewport.Width / LandscapeWidth;

    // Calculate the scaled width of one slice.
    var scaledWidth = (int)Math.Ceiling(imageSliceWidth * screenWidthScale);

    // Express the current point in time as "seconds passed since start of app".
    var currentSeconds = (float)gameTime.TotalGameTime.TotalSeconds;

    // Calculate how many seconds that has passed since the current microphone data was captured.
    var secondsPassed = currentSeconds - microphoneDataAppearedAtSeconds;

    // For a smooth move of the pixels, calculate the offset of the current microphone data
    // (where the offset is zero at the time of the new data arrived, and then growing up
    // one full width of a slice.
    var drawOffsetX = secondsPassed * pixelsPerSeconds;

    // Since it is not certain that the next microphone data will come before the current
    // slice has moved its full distance, the offset needs to be truncated so it doesn't
    // move too far.
    if (drawOffsetX &amp;gt; scaledWidth)
    {
        drawOffsetX = scaledWidth;
    }

    try
    {
        // Start draw the slices
        spriteBatch.Begin();

        // Start with one slice before the current one, wrap if necessary.
        var imageSlice = currentImageSlice.Previous &amp;#63;&amp;#63; lastImageSlice;

        // Prepare the rectangle to draw within, starting with the newest
        // slice far to the right of the screen (a bit outside, even).
        var destinationRectangle = new Rectangle(
            (int)(GraphicsDevice.Viewport.Width + scaledWidth - drawOffsetX),
            0,
            scaledWidth,
            GraphicsDevice.Viewport.Height);

        // Draw the slices in the linked list one by one from the right
        // to the left (from the newest sample to the oldest)
        // and move the destinationRectangle one slice-width at a time
        // until the full screen is covered.
        while (destinationRectangle.X &amp;gt; -scaledWidth)
        {
            // Draw the current image slice.
            spriteBatch.Draw(imageSlice.Value, destinationRectangle, Color.White);

            // Move the destinationRectangle one step to the left.
            destinationRectangle.X -= scaledWidth;

            // Select the previous image slice to draw next time, wrap if necessary.
            imageSlice = imageSlice.Previous &amp;#63;&amp;#63; lastImageSlice;
        }
    }
    finally
    {
        // Drawing done.
        spriteBatch.End();
    }

    base.Draw(gameTime);
}
&lt;/pre&gt;
&lt;div&gt;The RenderSamples&amp;nbsp;is taking a RenderTarget2D as an argument, which is the texture to be drawn on. The routine iterates through the samples and render them one by one.&amp;nbsp;&lt;/div&gt;
&lt;pre class="brush: csharp"&gt;private void RenderSamples(RenderTarget2D target)
{
    try
    {
        // Redirect the drawing to the given target.
        GraphicsDevice.SetRenderTarget(target);

        // Clear the target slice.
        GraphicsDevice.Clear(Color.Black);

        // Begin to draw. Use Additive for an interesting effect.
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);

        // The X-variable points out to which column of pixels to
        // draw on.
        var x = 0;

        // Since the byte-array buffer really holds 16-bit samples, the actual
        // number of samples in one buffer is the number of bytes divided by two.
        var sampleCount = microphoneData.Length / 2;

        // The index of the current sample in the microphone buffer.
        var sampleIndex = 0;

        // The vertical mid point of the image (the Y-position
        // of a zero-sample and the height of the loudest sample).
        var halfHeight = imageSliceHeight / 2;

        // The maximum number of a 16-bit signed integer.
        // Dividing a signed 16-bit integer (the range -32768..32767)
        // by this value will give a value in the range of -1 (inclusive) to 1 (exclusive).
        const float SampleFactor = 32768f;

        // Iterate through the samples and render them on the image.
        for (var i = 0; i &amp;lt; sampleCount; i++)
        {
            // Increment the X-coordinate each time 'samplesPerPixel' pixels
            // has been drawn.
            if ((i &amp;gt; 0) &amp;amp;&amp;amp; ((i % samplesPerPixel) == 0))
            {
                x++;
            }

            // Convert the current sample (16-bit value) from the byte-array to a
            // floating point value in the range of -1 (inclusive) to 1 (exclusive).
            var sampleValue = BitConverter.ToInt16(microphoneData, sampleIndex) / SampleFactor;

            // Scale the sampleValue to its corresponding height in pixels.
            var sampleHeight = (int)Math.Abs(sampleValue * halfHeight);

            // The top of the column of pixels.
            // A positive sample should be drawn from the center and upwards,
            // and a negative sample from the center and downwards.
            // Since a rectangle is used to describe the "pixel column", the
            // top must be modified depending on the sign of the sample (positive/negative).
            var y = (sampleValue &amp;lt; 0)
                ? halfHeight
                : halfHeight - sampleHeight;

            // Create the 1 pixel wide rectangle corresponding to the sample.
            var destinationRectangle = new Rectangle(x, y, 1, sampleHeight);

            // Draw using the white pixel (stretching it to fill the rectangle).
            spriteBatch.Draw(
                whitePixelTexture,
                destinationRectangle,
                sampleColor);

            // Step the two bytes-sample.
            sampleIndex += 2;
        }
    }
    finally
    {
        // Drawing done.
        spriteBatch.End();

        // Restore the normal rendering target (the screen).
        GraphicsDevice.SetRenderTarget(null);
    }
}&amp;nbsp;&lt;/pre&gt;</description>
      <link>http://laserbrain.se/post/2012/03/11/Rendering-the-audio-captured-by-a-Windows-Phone-device.aspx</link>
      <comments>http://laserbrain.se/post/2012/03/11/Rendering-the-audio-captured-by-a-Windows-Phone-device.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=1a5d7c67-2bc6-4bfb-b939-a6c41027ba54</guid>
      <pubDate>Sun, 11 Mar 2012 17:11:00 +0200</pubDate>
      <category>C#</category>
      <category>WP7</category>
      <category>XNA</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=1a5d7c67-2bc6-4bfb-b939-a6c41027ba54</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=1a5d7c67-2bc6-4bfb-b939-a6c41027ba54</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2012/03/11/Rendering-the-audio-captured-by-a-Windows-Phone-device.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=1a5d7c67-2bc6-4bfb-b939-a6c41027ba54</wfw:commentRss>
    </item>
    <item>
      <title>The Challenge of the Day</title>
      <description>&lt;p&gt;Dear fellow developers,&lt;/p&gt;
&lt;p&gt;Please create an instance of the following class (using any type as T):&lt;/p&gt;
&lt;pre class="brush: csharp"&gt;class Foo&amp;lt;T&amp;gt;
    where T : Foo&amp;lt;T&amp;gt;
{
}&lt;/pre&gt;
&lt;p&gt;You may use any technique you like; plain "new MyClass...", using reflection, hacking MSIL, whatever.&lt;/p&gt;
&lt;p&gt;Good luck! :)&lt;/p&gt;</description>
      <link>http://laserbrain.se/post/2012/03/10/The-Challenge-of-the-Day.aspx</link>
      <comments>http://laserbrain.se/post/2012/03/10/The-Challenge-of-the-Day.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=fead30e8-c236-4f66-9101-33509b1c7625</guid>
      <pubDate>Sat, 10 Mar 2012 16:30:00 +0200</pubDate>
      <category>C#</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=fead30e8-c236-4f66-9101-33509b1c7625</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=fead30e8-c236-4f66-9101-33509b1c7625</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2012/03/10/The-Challenge-of-the-Day.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=fead30e8-c236-4f66-9101-33509b1c7625</wfw:commentRss>
    </item>
    <item>
      <title>Follow the rules when developing your Windows Phone app</title>
      <description>&lt;p&gt;Roughly one month after the first version of &lt;a title="laserbrain.se/page/badgersrock.aspx" href="laserbrain.se/page/badgersrock.aspx" target="_blank"&gt;Badgers Rock&lt;/a&gt; had passed the certification cycle and happily thrived in the Windows Phone Marketplace, I received the following e-mail from one of the app testers:&amp;nbsp;&lt;/p&gt;
&lt;p style="padding-left: 30px;"&gt;&lt;span style="font-family: 'courier new', courier;"&gt;Dear Windows Phone Marketplace Developer,&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;It has been determined through certification testing that the application indicated in the subject line has the following problem(s):&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;5.2.4.4&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;Pass with Notes: Pressing the device back button has no effect on the application.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;Steps to Reproduce:&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;1. Launch the application.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;2. Select "PLAY!".&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;3. Select a stage.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;4. Press the device Back button.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;5. A pause menu is displayed.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;6. Press the device Back button again.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;7. Observe the application the application does not respond.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;The application passes certification, however the reported issues need to be resolved in the next update to the application.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;ACTIONS REQUIRED: Please address the issues listed above, review the Windows Phone Application Certification requirements (&lt;a title="http://go.microsoft.com/?linkid=9730556" href="http://go.microsoft.com/?linkid=9730556" target="_blank"&gt;http://go.microsoft.com/?linkid=9730556&lt;/a&gt;) and resubmit your updated application for certification testing. &amp;nbsp;Future application update submissions will fail certification testing if the same issue(s) are not corrected.&lt;br /&gt;P&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;LEASE DO NOT REPLY TO THIS MESSAGE. For further assistance, please visit &lt;a title="http://forums.create.msdn.com/forums/98.aspx" href="http://forums.create.msdn.com/forums/98.aspx" target="_blank"&gt;Developing for Windows Phone 7 forum&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;Thanks,&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;Sam&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: 'courier new', courier;"&gt;Windows Phone Marketplace Certification&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Sam was absolutely right about his finding, but it was in fact a design decision from my side. In the described scenario you are in an ongoing game and then you press the physical Back button on the phone. The game is now paused and you are presented with a couple of options; basically "do you want to end the game or resume?". So, at this point, how should I handle the Back-button? It was of course out of the question to &lt;strong&gt;end&lt;/strong&gt; the game; it's way too simple to accidentally touch this button (depending on how you are holding the device). So making a double miss-tap ending the game would not make any hardcore Badgers Rock-gamer happy. So should the game resume then? This was the only obvious option left (making the game respond to the click), but I didn't really like that anyway because it would not be 100% clear to the player what the Back button would actually do in this case. So I decided to ignore the Back button in this screen and force the player to take an active decision choosing from the spelled-out options on screen -- again, to make it perfectly clear for the player what will happen.&lt;/p&gt;
&lt;p&gt;Then this message came from which a couple of things can be noticed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First of all, Microsoft is (for the first time?) really anxious that all the applications are behaving in a consistent way, not only giving guidelines. This is a good thing.&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Secondly, it's not only automatic testing being performed on your submitted app. This is also a good thing. Although it doesn't seem as every release is tested manually since I had maybe four versions successfully passing the certification cycle when I got this message -- and the behavior in question had been there from the start.&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Also, at the point in time when I received this message there was no certification phase currently going on; my latest release had already been passed successfully some time ago. Does this mean that the testers are doing the manual testing as a parallel task on top of the (more or less) automatic certification cycle? My app was presumably put in a queue and rested there for a month until someone (Sam!) had time to perform the manual testing.&lt;br /&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;A side note is the not-so-professional link they've included in the e-mail (&lt;a title="http://go.microsoft.com/?linkid=9730556" href="http://go.microsoft.com/?linkid=9730556" target="_blank"&gt;http://go.microsoft.com/?linkid=9730556&lt;/a&gt;). This points to a PDF-file you'll have to download that's only telling you that the link has been moved (and of course &lt;a title="http://go.microsoft.com/fwlink/?LinkID=183220" href="http://go.microsoft.com/fwlink/?LinkID=183220" target="_blank"&gt;where to&lt;/a&gt;). Suprising? Not really. Elegant? Well, not really...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Of course I should follow the requirements of always having a responsive Back-button!&lt;br /&gt;So now during game-play, the Back-button will basically toggle pause mode on and off.&lt;/p&gt;</description>
      <link>http://laserbrain.se/post/2012/02/09/Follow-the-rules-when-developing-your-Windows-Phone-app.aspx</link>
      <comments>http://laserbrain.se/post/2012/02/09/Follow-the-rules-when-developing-your-Windows-Phone-app.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=550a85ad-e302-46ce-a3db-a5621a64150b</guid>
      <pubDate>Thu, 09 Feb 2012 16:25:00 +0200</pubDate>
      <category>General</category>
      <category>WP7</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=550a85ad-e302-46ce-a3db-a5621a64150b</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=550a85ad-e302-46ce-a3db-a5621a64150b</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2012/02/09/Follow-the-rules-when-developing-your-Windows-Phone-app.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=550a85ad-e302-46ce-a3db-a5621a64150b</wfw:commentRss>
    </item>
    <item>
      <title>Badgers Rock</title>
      <description>&lt;table style="width: 100%;" border="0" cellspacing="0" cellpadding="2"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" width="100"&gt;
&lt;p&gt;&lt;img src="\images\badgersrock\largetile.png" alt="Windows Phone Tile" width="100" height="100" /&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td valign="top" width="4"&gt;&amp;nbsp;&lt;/td&gt;
&lt;td valign="top"&gt;
&lt;p&gt;Badgers Rock is a small, completely free (no ads) Windows Phone game I created using the XNA framework during the autumn of 2011.&lt;br /&gt;You can find it on the&amp;nbsp;&lt;a title="(The Windows Phone Marketplace)" href="http://windowsphone.com/s?appid=0486a9d0-003a-413d-adbe-8b5f274cc7bf" target="_blank"&gt;Windows Phone Marketplace&lt;/a&gt;&amp;nbsp;and on&amp;nbsp;&lt;a title="http://www.facebook.com/pages/Badgers-Rock/285701098149575" href="http://www.facebook.com/pages/Badgers-Rock/285701098149575" target="_blank"&gt;Facebook&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The latest released version (V1.4) has both an online high score and cool retro sound.&lt;/p&gt;
&lt;p&gt;There was a contest arranged by the&amp;nbsp;&lt;a title="Windows Phone Sverige" href="http://www.facebook.com/wpswe" target="_blank"&gt;"Windows Phone Sweden" Facebook page&lt;/a&gt;, that ended on February 3, 2012, and the Winner Niklas Dahlman successfully completed all thirty levels with "three stars" on each levels. Not an easy task considering some levels are really hard and time-demanding to complete. He won a Nokia Bluetooth Stereo Headset BH-111. Congratulations!&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The online highscore is provided and hosted by&amp;nbsp;&lt;a title="http://mogade.com/" href="http://mogade.com/" target="_blank"&gt;http://mogade.com/&lt;/a&gt;. It is free, open source and have a very easy API to integrate. You can choose to host your data yourself or let them do it for you. Really nice.&lt;/p&gt;
&lt;p&gt;All the sounds in the game were taken from the&amp;nbsp;&lt;a title="http://www.freesound.org/" href="http://www.freesound.org/" target="_blank"&gt;http://www.freesound.org/&lt;/a&gt;. Here is a complete&amp;nbsp;&lt;a title="Badgers Rock Sounds" href="http://laserbrain.se/page/badgersrocksounds.aspx"&gt;list&lt;/a&gt; of the sounds that I used in the game.&lt;/p&gt;</description>
      <link>http://laserbrain.se/post/2012/02/08/badgersrock.aspx</link>
      <comments>http://laserbrain.se/post/2012/02/08/badgersrock.aspx#disqus_thread</comments>
      <guid>http://laserbrain.se/post.aspx?id=9c1f44f6-0682-40b6-9bdb-c55335546938</guid>
      <pubDate>Wed, 08 Feb 2012 17:54:00 +0200</pubDate>
      <category>C#</category>
      <category>WP7</category>
      <category>XNA</category>
      <dc:publisher>Mattias</dc:publisher>
      <pingback:server>http://laserbrain.se/pingback.axd</pingback:server>
      <pingback:target>http://laserbrain.se/post.aspx?id=9c1f44f6-0682-40b6-9bdb-c55335546938</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://laserbrain.se/trackback.axd?id=9c1f44f6-0682-40b6-9bdb-c55335546938</trackback:ping>
      <wfw:comment>http://laserbrain.se/post/2012/02/08/badgersrock.aspx#disqus_thread</wfw:comment>
      <wfw:commentRss>http://laserbrain.se/syndication.axd?post=9c1f44f6-0682-40b6-9bdb-c55335546938</wfw:commentRss>
    </item>
  </channel>
</rss>