<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Omer van Kloeten's .NET Zen</title><link>http://weblogs.asp.net/okloeten/default.aspx</link><description>Programming is life, the rest is mere details</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Hiatus</title><link>http://weblogs.asp.net/okloeten/archive/2009/02/23/6924135.aspx</link><pubDate>Mon, 23 Feb 2009 12:48:45 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6924135</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6924135</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2009/02/23/6924135.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://www.flickr.com/photos/omervk/1166629131/in/set-72157601542639193/"&gt;&lt;img title="" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 0px 10px; border-left: 0px; border-bottom: 0px" height="240" alt="" src="http://weblogs.asp.net/blogs/okloeten/image_14AD2338.png" width="215" align="right" border="0" /&gt;&lt;/a&gt; In 15 days’ time, I’ll be taking off on a four months long trip around the world. I’ll be hiking and travelling my way through Japan (March-May), the United States of America (May-July) and the Netherlands (July). I’ve been planning this trip for a long time now, since I came back from Japan for the first time in August 2007 (the photo on the right was taken near the Tsurugaoka Hachiman-gu Shrine in &lt;a href="http://wikitravel.org/en/Kamakura"&gt;Kamakura&lt;/a&gt;), and have taken a long leave from work to accomplish it. What this means is that from March 10th you can expect a long pause in my semi-regular updates.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;I am not dead&lt;/strong&gt; :)&lt;/p&gt;  &lt;p&gt;If you’re a reader who lives along my path and want to meet up for a beer and a lively chat, ping me via the &lt;a href="http://weblogs.asp.net/okloeten/contact.aspx"&gt;contact form&lt;/a&gt;. I’ll be reading my emails every once in a while, so don’t worry if you only get an answer after a few days.&lt;/p&gt;  &lt;p&gt;If you want to read about my trip, I’ll be posting regular updates to my &lt;a href="http://blog.omervk.com/"&gt;new travel blog&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6924135" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Personal/default.aspx">Personal</category></item><item><title>Circumventing the KB957543 .NET 3.5 SP1 Regression Bug</title><link>http://weblogs.asp.net/okloeten/archive/2009/01/29/6868393.aspx</link><pubDate>Thu, 29 Jan 2009 16:23:18 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6868393</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6868393</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2009/01/29/6868393.aspx#comments</comments><description>&lt;p&gt;A couple of days ago I hit a regression bug in .NET 3.5 SP1, in which when you have a generic class that implements ISerializable and has static variables – you can not serialize it using a BinaryFormatter without your application either hanging (x86) or raising an exception (x64 – a TargetInvocationException containing an OutOfMemoryException). This only happens if you use a reference type as a generic argument.&lt;/p&gt;  &lt;p&gt;It’s already &lt;a href="http://www.hanselman.com/blog/UpdateOnNETFramework35SP1AndWindowsUpdate.aspx"&gt;well known&lt;/a&gt;, but I have yet to find a workaround documented anywhere. You could simply install the &lt;a href="http://code.msdn.microsoft.com/KB957543"&gt;hotfix&lt;/a&gt;, but well, I wouldn’t if I were you – it hasn’t been thoroughly tested yet. Moreover, you might not even be able to do so due to either internal politics, strict IT rules or the fact that you simply do not have control over the hosting server.&lt;/p&gt;  &lt;p&gt;Let’s take the simplest class that causes the issue:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;[&lt;span style="color: #2b91af"&gt;Serializable&lt;/span&gt;]
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;ISerializable
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private static int &lt;/span&gt;list = 0;

    &lt;span style="color: blue"&gt;public &lt;/span&gt;MyClass()
    {
    }

    &lt;span style="color: blue"&gt;protected &lt;/span&gt;MyClass(&lt;span style="color: #2b91af"&gt;SerializationInfo &lt;/span&gt;info, &lt;span style="color: #2b91af"&gt;StreamingContext &lt;/span&gt;context)
    {
    }

    &lt;span style="color: blue"&gt;void &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ISerializable&lt;/span&gt;.GetObjectData(&lt;span style="color: #2b91af"&gt;SerializationInfo &lt;/span&gt;info, &lt;span style="color: #2b91af"&gt;StreamingContext &lt;/span&gt;context)
    {
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When using the class as such:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;MemoryStream &lt;/span&gt;stream = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;())
{
    &lt;span style="color: #2b91af"&gt;BinaryFormatter &lt;/span&gt;formatter = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BinaryFormatter&lt;/span&gt;();
    formatter.Serialize(stream, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;());
    stream.Position = 0;
    &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; item = (&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;)formatter.Deserialize(stream);
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The last line will hit the bug.&lt;/p&gt;

&lt;p&gt;To work around this issue, simply move your static variables into a new subclass:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;[&lt;span style="color: #2b91af"&gt;Serializable&lt;/span&gt;]
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;ISerializable
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;KB957543
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public static int &lt;/span&gt;list = 0;
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;MyClass()
    {
    }

    &lt;span style="color: blue"&gt;protected &lt;/span&gt;MyClass(&lt;span style="color: #2b91af"&gt;SerializationInfo &lt;/span&gt;info, &lt;span style="color: #2b91af"&gt;StreamingContext &lt;/span&gt;context)
    {
    }

    &lt;span style="color: blue"&gt;void &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ISerializable&lt;/span&gt;.GetObjectData(&lt;span style="color: #2b91af"&gt;SerializationInfo &lt;/span&gt;info, &lt;span style="color: #2b91af"&gt;StreamingContext &lt;/span&gt;context)
    {
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can still access all of your static variables and you don’t hit the bug.&lt;/p&gt;

&lt;p&gt;Note that when you use anonymous methods or lambdas, they are cached as static variables of the type, meaning that you will have to manually type all of your lambdas.&lt;/p&gt;

&lt;p&gt;Here’s an example of such a type that is prone to the bug:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;[&lt;span style="color: #2b91af"&gt;Serializable&lt;/span&gt;]
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;ISerializable
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public &lt;/span&gt;MyClass()
    {
    }

    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;Concat(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; numbers)
    {
        &lt;span style="color: blue"&gt;return string&lt;/span&gt;.Join(&lt;span style="color: #a31515"&gt;&amp;quot;, &amp;quot;&lt;/span&gt;, numbers.Select(i =&amp;gt; i.ToString()).ToArray());
    }

    &lt;span style="color: blue"&gt;protected &lt;/span&gt;MyClass(&lt;span style="color: #2b91af"&gt;SerializationInfo &lt;/span&gt;info, &lt;span style="color: #2b91af"&gt;StreamingContext &lt;/span&gt;context)
    {
    }

    &lt;span style="color: blue"&gt;void &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ISerializable&lt;/span&gt;.GetObjectData(&lt;span style="color: #2b91af"&gt;SerializationInfo &lt;/span&gt;info, &lt;span style="color: #2b91af"&gt;StreamingContext &lt;/span&gt;context)
    {
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we look through Reflector, we can see that there is a cached delegate in our type:&lt;/p&gt;

&lt;p&gt;&lt;img title="" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="186" alt="" src="http://weblogs.asp.net/blogs/okloeten/image_292AD855.png" width="589" border="0" /&gt; &lt;/p&gt;

&lt;p&gt;Since this is a static member, it makes the type susceptible to the bug and we now need to manually create the cached member ourselves:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;[&lt;span style="color: #2b91af"&gt;Serializable&lt;/span&gt;]
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;ISerializable
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;KB957543
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; ToString = i =&amp;gt; i.ToString();
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;MyClass()
    {
    }

    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;Concat(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; numbers)
    {
        &lt;span style="color: blue"&gt;return string&lt;/span&gt;.Join(&lt;span style="color: #a31515"&gt;&amp;quot;, &amp;quot;&lt;/span&gt;, numbers.Select(&lt;span style="color: #2b91af"&gt;KB957543&lt;/span&gt;.ToString).ToArray());
    }

    &lt;span style="color: blue"&gt;protected &lt;/span&gt;MyClass(&lt;span style="color: #2b91af"&gt;SerializationInfo &lt;/span&gt;info, &lt;span style="color: #2b91af"&gt;StreamingContext &lt;/span&gt;context)
    {
    }

    &lt;span style="color: blue"&gt;void &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ISerializable&lt;/span&gt;.GetObjectData(&lt;span style="color: #2b91af"&gt;SerializationInfo &lt;/span&gt;info, &lt;span style="color: #2b91af"&gt;StreamingContext &lt;/span&gt;context)
    {
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6868393" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/How+To/default.aspx">How To</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Advices/default.aspx">Advices</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Let SQL Server Tell You Which Indexes to Rebuild</title><link>http://weblogs.asp.net/okloeten/archive/2009/01/05/6819737.aspx</link><pubDate>Mon, 05 Jan 2009 11:37:45 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6819737</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6819737</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2009/01/05/6819737.aspx#comments</comments><description>&lt;p&gt;When index fragmentation becomes too high, indexes will be very inefficient. Other than planning a good index design, you should &lt;a href="http://msdn.microsoft.com/library/ms188388.aspx"&gt;rebuild / reorganize your indexes&lt;/a&gt; every once in a while.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: red"&gt;'ALTER INDEX [' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;ix&lt;span style="color: gray"&gt;.&lt;/span&gt;name &lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: red"&gt;'] ON [' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;s&lt;span style="color: gray"&gt;.&lt;/span&gt;name &lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: red"&gt;'].[' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;t&lt;span style="color: gray"&gt;.&lt;/span&gt;name &lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: red"&gt;'] ' &lt;/span&gt;&lt;span style="color: gray"&gt;+
       &lt;/span&gt;&lt;span style="color: blue"&gt;CASE WHEN &lt;/span&gt;ps&lt;span style="color: gray"&gt;.&lt;/span&gt;avg_fragmentation_in_percent &lt;span style="color: gray"&gt;&amp;gt; &lt;/span&gt;40 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;'REBUILD' &lt;/span&gt;&lt;span style="color: blue"&gt;ELSE &lt;/span&gt;&lt;span style="color: red"&gt;'REORGANIZE' &lt;/span&gt;&lt;span style="color: blue"&gt;END &lt;/span&gt;&lt;span style="color: gray"&gt;+
       &lt;/span&gt;&lt;span style="color: blue"&gt;CASE WHEN &lt;/span&gt;pc&lt;span style="color: gray"&gt;.&lt;/span&gt;partition_count &lt;span style="color: gray"&gt;&amp;gt; &lt;/span&gt;1 &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: red"&gt;' PARTITION = ' &lt;/span&gt;&lt;span style="color: gray"&gt;+ &lt;/span&gt;&lt;span style="color: magenta"&gt;cast&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ps&lt;span style="color: gray"&gt;.&lt;/span&gt;partition_number &lt;span style="color: blue"&gt;as nvarchar&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: magenta"&gt;max&lt;/span&gt;&lt;span style="color: gray"&gt;)) &lt;/span&gt;&lt;span style="color: blue"&gt;ELSE &lt;/span&gt;&lt;span style="color: red"&gt;'' &lt;/span&gt;&lt;span style="color: blue"&gt;END
FROM   &lt;/span&gt;&lt;span style="color: green"&gt;sys&lt;/span&gt;&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: green"&gt;indexes &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;ix &lt;span style="color: gray"&gt;INNER JOIN &lt;/span&gt;&lt;span style="color: green"&gt;sys&lt;/span&gt;&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: green"&gt;tables &lt;/span&gt;t
           &lt;span style="color: blue"&gt;ON &lt;/span&gt;t&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: magenta"&gt;object_id &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;ix&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: magenta"&gt;object_id
       &lt;/span&gt;&lt;span style="color: gray"&gt;INNER JOIN &lt;/span&gt;&lt;span style="color: green"&gt;sys&lt;/span&gt;&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: green"&gt;schemas &lt;/span&gt;s
           &lt;span style="color: blue"&gt;ON &lt;/span&gt;t&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: magenta"&gt;schema_id &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;s&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: magenta"&gt;schema_id
       &lt;/span&gt;&lt;span style="color: gray"&gt;INNER JOIN (&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: magenta"&gt;object_id&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;index_id&lt;span style="color: gray"&gt;, &lt;/span&gt;avg_fragmentation_in_percent&lt;span style="color: gray"&gt;, &lt;/span&gt;partition_number
                   &lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: green"&gt;sys&lt;/span&gt;&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: green"&gt;dm_db_index_physical_stats &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: magenta"&gt;DB_ID&lt;/span&gt;&lt;span style="color: gray"&gt;(), NULL, NULL, NULL, NULL)) &lt;/span&gt;ps
           &lt;span style="color: blue"&gt;ON &lt;/span&gt;t&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: magenta"&gt;object_id &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;ps&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: magenta"&gt;object_id &lt;/span&gt;&lt;span style="color: gray"&gt;AND &lt;/span&gt;ix&lt;span style="color: gray"&gt;.&lt;/span&gt;index_id &lt;span style="color: gray"&gt;= &lt;/span&gt;ps&lt;span style="color: gray"&gt;.&lt;/span&gt;index_id
       &lt;span style="color: gray"&gt;INNER JOIN (&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: magenta"&gt;object_id&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;index_id&lt;span style="color: gray"&gt;, &lt;/span&gt;&lt;span style="color: magenta"&gt;COUNT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;DISTINCT &lt;/span&gt;partition_number&lt;span style="color: gray"&gt;) &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;partition_count
                   &lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: green"&gt;sys&lt;/span&gt;&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: green"&gt;partitions
                   &lt;/span&gt;&lt;span style="color: blue"&gt;GROUP BY &lt;/span&gt;&lt;span style="color: magenta"&gt;object_id&lt;/span&gt;&lt;span style="color: gray"&gt;, &lt;/span&gt;index_id&lt;span style="color: gray"&gt;) &lt;/span&gt;pc
           &lt;span style="color: blue"&gt;ON &lt;/span&gt;t&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: magenta"&gt;object_id &lt;/span&gt;&lt;span style="color: gray"&gt;= &lt;/span&gt;pc&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: magenta"&gt;object_id &lt;/span&gt;&lt;span style="color: gray"&gt;AND &lt;/span&gt;ix&lt;span style="color: gray"&gt;.&lt;/span&gt;index_id &lt;span style="color: gray"&gt;= &lt;/span&gt;pc&lt;span style="color: gray"&gt;.&lt;/span&gt;index_id
&lt;span style="color: blue"&gt;WHERE  &lt;/span&gt;ps&lt;span style="color: gray"&gt;.&lt;/span&gt;avg_fragmentation_in_percent &lt;span style="color: gray"&gt;&amp;gt; &lt;/span&gt;10 &lt;span style="color: gray"&gt;AND
       &lt;/span&gt;ix&lt;span style="color: gray"&gt;.&lt;/span&gt;name &lt;span style="color: gray"&gt;IS NOT NULL
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above query will give you a list of recommended index rebuild / reorganize &lt;a href="http://msdn.microsoft.com/library/ms188388.aspx"&gt;statements&lt;/a&gt; for your database, according &lt;a href="http://blog.sqlauthority.com/"&gt;Pinal Dave&lt;/a&gt;’s &lt;a href="http://blog.sqlauthority.com/2007/12/22/sql-server-difference-between-index-rebuild-and-index-reorganize-explained-with-t-sql-script/"&gt;10-40 rule&lt;/a&gt;, although you are welcome to tweak it to your liking. It supports non-partitioned as well as partitioned indexes. If you want a more intense check for fragmentation, change the last &lt;code&gt;NULL&lt;/code&gt; in the &lt;code&gt;&lt;a href="http://msdn.microsoft.com/library/ms188917.aspx"&gt;dm_db_index_physical_stats&lt;/a&gt;&lt;/code&gt; call to 'SAMPLED' or even 'DETAILED' (include quotes).&lt;/p&gt;

&lt;p&gt;It’s a handy little tool for database administrators and saves a lot of the hassle of monitoring index fragmentation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Added multi-schema support as suggested by MJ12 and another check for null index names.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6819737" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>SQL Server Management Studio 2008 IntelliSense Doesn't Recognize Special Characters</title><link>http://weblogs.asp.net/okloeten/archive/2009/01/05/6819287.aspx</link><pubDate>Mon, 05 Jan 2009 09:16:12 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6819287</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6819287</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2009/01/05/6819287.aspx#comments</comments><description>&lt;p&gt;&lt;img title="ssms" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 5px 10px 5px 5px; border-right-width: 0px" height="156" alt="ssms" src="http://weblogs.asp.net/blogs/okloeten/ssms_0C00114E.png" width="286" align="left" border="0" /&gt;&lt;/p&gt;  &lt;p&gt;I just filed &lt;a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=390749"&gt;a new bug with Microsoft Connect&lt;/a&gt;. I certainly hope this one doesn’t get shrugged off like many of my other bugs did.&lt;/p&gt;  &lt;p&gt;Want to reproduce it yourselves? Just create a table with a character that can only be valid inside the context of brackets (like a comma or braces) and then try to select from it. Don’t keep the CREATE clauses in the same query window or it might just work. See the screenshot below.&lt;/p&gt;  &lt;p&gt;This is very frustrating because this means that not only is there no IntelliSense for these objects, but IntelliSense now gets in the way of actual querying as it will autocomplete incorrect names.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6819287" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Adobe AIR and Hebrew Fonts</title><link>http://weblogs.asp.net/okloeten/archive/2008/12/24/6805823.aspx</link><pubDate>Wed, 24 Dec 2008 08:07:57 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6805823</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6805823</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/12/24/6805823.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/HebrewTwhirl_316513D9.png"&gt;&lt;img title="Twhirl" style="border-right: 0px; border-top: 0px; display: inline; margin: 5px 5px 5px 10px; border-left: 0px; border-bottom: 0px" height="202" alt="Twhirl" src="http://weblogs.asp.net/blogs/okloeten/HebrewTwhirl_thumb_7A5B0FD2.png" width="130" align="right" border="0" /&gt;&lt;/a&gt;(I’ve noticed quite a few people were having this problem, so I decided to blog about it. This might have to do with all non-English fonts, but I experienced it only with Hebrew)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/TweetDeckHebrew_557548C6.png"&gt;&lt;img title="TweetDeck" style="border-right: 0px; border-top: 0px; display: inline; margin: 5px 10px 5px 5px; border-left: 0px; border-bottom: 0px" height="202" alt="TweetDeck" src="http://weblogs.asp.net/blogs/okloeten/TweetDeckHebrew_thumb_0B2A8E21.png" width="124" align="left" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The only reason I use Adobe AIR is for the Twitter application &lt;a href="http://www.twhirl.org/project/twhirl" target="_blank"&gt;Twhirl&lt;/a&gt;. However, I noticed the Hebrew text was incorrectly displayed (word order was reversed).&lt;/p&gt;  &lt;p&gt;I did not find a solution for this online and the guys at Twhirl didn’t know what to make of this.&lt;/p&gt;  &lt;p&gt;After toying with a few of the options, I finally found the answer – the fonts used by Twhirl were in Hebrew, but AIR wasn’t playing nicely with them. I switched from Calibri to Tahoma and found that the text was just fine.&lt;/p&gt;  &lt;p&gt;Both &lt;a href="http://twitter.com/effifuks/status/1059151241" target="_blank"&gt;@effifuks&lt;/a&gt; and &lt;a href="http://twitter.com/JonathanRauch/status/1075041688" target="_blank"&gt;@JonathanRauch&lt;/a&gt; (with TweetDeck, where he didn’t see Hebrew text at all – see the left screenshot) experienced the same issue.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6805823" width="1" height="1"&gt;</description></item><item><title>MSBuild Script to Compress All JavaScript Files in a Project</title><link>http://weblogs.asp.net/okloeten/archive/2008/12/18/6795069.aspx</link><pubDate>Thu, 18 Dec 2008 16:58:47 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6795069</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6795069</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/12/18/6795069.aspx#comments</comments><description>I’ve got one project in my solution which has a lot of JavaScript files and they keep on coming. We’ve been using the &lt;a href="http://developer.yahoo.com/yui/compressor/" target="_blank"&gt;YUI Compressor&lt;/a&gt; for quite a while and it’s proven an effective tool. After a lot of time of fiddling with the project’s MSBuild script, I came up with the following:   &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Target &lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;BeforeBuild&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;MakeDir&lt;br /&gt;&lt;/span&gt;&lt;span style="color: red"&gt;    Directories&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;compressed\%(Content.RelativeDir)&lt;/span&gt;&amp;quot;&lt;br /&gt;    &lt;span style="color: red"&gt;Condition&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;(%(Content.Extension) == '.js') And (!(Exists('compressed\%(Content.RelativeDir)')))&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Exec&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: red"&gt;Command&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;java -jar yuicompressor-x.y.z.jar --type js -o compressed\%(Content.Identity) %(Content.Identity)&lt;/span&gt;&amp;quot;&lt;br /&gt;    &lt;span style="color: red"&gt;Condition&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;%(Content.Extension) == '.js'&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;CreateItem&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: red"&gt;Include&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;compressed\%(Content.Identity)&lt;/span&gt;&amp;quot;&lt;br /&gt;    &lt;span style="color: red"&gt;Condition&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;%(Content.Extension) == '.js'&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Target&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The above takes all of the JavaScript files in your project, compresses them (in the same relative directories) into the &lt;em&gt;compressed&lt;/em&gt; directory and then adds them to the project, in case it gets deployed anywhere.&lt;/p&gt;

&lt;p&gt;Note that I’m using the &lt;em&gt;Extension&lt;/em&gt;, &lt;em&gt;Identity&lt;/em&gt; and &lt;em&gt;RelativeDir&lt;/em&gt; &lt;a href="http://msdn.microsoft.com/library/ms164313.aspx" target="_blank"&gt;well-known item metadata attributes&lt;/a&gt; in order to impose batching, since batches causes loops instead of the string concatenation that happens when you reference the items themselves.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6795069" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/MSBuild/default.aspx">MSBuild</category></item><item><title>Your Mouth Says Windows-1255, But Your Eyes Say ISO-8859-1</title><link>http://weblogs.asp.net/okloeten/archive/2008/12/18/6795056.aspx</link><pubDate>Thu, 18 Dec 2008 16:45:34 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6795056</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6795056</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/12/18/6795056.aspx#comments</comments><description>&lt;h4&gt;I recently wrote an engine that gets XML files stored at our clients’ servers using HTTP requests. One of our clients decided to serve the XML file with one encoding and encode the file itself with another. This posed a problem to XDocument.&lt;/h4&gt;  &lt;p&gt;The client decided to encode their XML using the Windows-1255 encoding (Hebrew), noting the encoding correctly in the XML’s declaration, but served the file stating the ISO-8859-1 (Latin) encoding. This meant that I couldn’t just use &lt;em&gt;XDocument&lt;/em&gt;’s normal &lt;em&gt;Load&lt;/em&gt; method to load directly from the stream because &lt;strong&gt;XDocument looks at the HTTP headers and takes the document’s encoding from them&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;Here’s a snippet of the code I used to get over that:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;HttpWebResponse &lt;/span&gt;response = (&lt;span style="color: #2b91af"&gt;HttpWebResponse&lt;/span&gt;)request.GetResponse())
{
    &lt;span style="color: green"&gt;// Use response's charset.
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;encoding = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.GetEncoding(&lt;span style="color: #a31515"&gt;&amp;quot;ISO-8859-1&amp;quot;&lt;/span&gt;);

    &lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(response.CharacterSet))
        encoding = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.GetEncoding(response.CharacterSet);

    &lt;span style="color: blue"&gt;byte&lt;/span&gt;[] bytes = ReadStream(response.GetResponseStream());

    &lt;span style="color: green"&gt;// Get the XML with the response's charset.
    &lt;/span&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;xml = &lt;span style="color: blue"&gt;new string&lt;/span&gt;(encoding.GetChars(bytes));
    &lt;span style="color: blue"&gt;int &lt;/span&gt;endOfDeclaration = xml.IndexOf(&lt;span style="color: #a31515"&gt;&amp;quot;?&amp;gt;&amp;quot;&lt;/span&gt;);

    &lt;span style="color: blue"&gt;if &lt;/span&gt;(endOfDeclaration != -1)
    {
        &lt;span style="color: green"&gt;// Try to find out the encoding from the declaration.
        &lt;/span&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;decl = xml.Substring(0, endOfDeclaration + 2) + &lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;duperoot /&amp;gt;&amp;quot;&lt;/span&gt;;
        &lt;span style="color: #2b91af"&gt;XDocument &lt;/span&gt;declDoc = &lt;span style="color: #2b91af"&gt;XDocument&lt;/span&gt;.Parse(decl);
        &lt;span style="color: blue"&gt;var &lt;/span&gt;docEncoding = &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.GetEncoding(declDoc.Declaration.Encoding);

        &lt;span style="color: blue"&gt;if &lt;/span&gt;(docEncoding == encoding)
            &lt;span style="color: blue"&gt;return &lt;/span&gt;xml;
        &lt;span style="color: blue"&gt;else
            return new string&lt;/span&gt;(docEncoding.GetChars(bytes));
    }
    &lt;span style="color: blue"&gt;else
    &lt;/span&gt;{
        &lt;span style="color: green"&gt;// Not XML or something... Send up.
&lt;/span&gt;    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What I did here was to create a new document with the original XML’s declaration (the Latin characters which make up the XML’s declaration always have the same byte position), add a dupe root and parse that to get the name of the encoding used by the document. I then use that encoding to decode the document correctly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note &lt;/strong&gt;that I’m using ISO-8859-1 as the default response’s encoding, since that is what HTTP’s specification demands.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6795056" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/How+To/default.aspx">How To</category></item><item><title>HTML to JavaScript HTML DOM Converter</title><link>http://weblogs.asp.net/okloeten/archive/2008/12/18/6795039.aspx</link><pubDate>Thu, 18 Dec 2008 16:20:46 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6795039</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6795039</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/12/18/6795039.aspx#comments</comments><description>&lt;p&gt;One of our products demands converting quite a large amount of HTML to equivalent scripts, written in JavaScript that create said HTML. Looking around the Internet, I found no tool that will automate this process for me, so I went ahead and created this simple little application.&lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/htmltojsdom_37FDB96F.png"&gt;&lt;img title="htmltojsdom" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="603" alt="htmltojsdom" src="http://weblogs.asp.net/blogs/okloeten/htmltojsdom_thumb_3AE2F855.png" width="616" border="0"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;I've written it using &lt;a href="http://www.codeplex.com/htmlagilitypack"&gt;HTML Agility Pack&lt;/a&gt; and &lt;a href="http://www.codeproject.com/KB/recipes/CSSParser.aspx"&gt;Simple CSS Parser&lt;/a&gt;. It’s hardly perfect and might incorrectly reference attributes, but I’ve tweaked it long enough for it to work, I’m guessing, 95% of the time.&lt;/p&gt; &lt;p&gt;The source is not yet included (I want to set it up as a project on CodePlex later on), but for now &lt;a href="http://blogs.microsoft.co.il/files/folders/195021/download.aspx" target="_blank"&gt;you can download it from here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6795039" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Misc/default.aspx">Tools: Misc</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/JavaScript/default.aspx">JavaScript</category></item><item><title>The Death of System.DateTime?</title><link>http://weblogs.asp.net/okloeten/archive/2008/12/18/6795009.aspx</link><pubDate>Thu, 18 Dec 2008 15:56:38 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6795009</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6795009</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/12/18/6795009.aspx#comments</comments><description>&lt;h4&gt;It’s been a few months since I started getting acquainted with the &lt;em&gt;System.DateTimeOffset&lt;/em&gt; type and I can honestly say I don’t see any reason to use &lt;em&gt;System.DateTime&lt;/em&gt; anymore.&lt;/h4&gt;  &lt;p&gt;I’ve even gone as far as ask whether &lt;a href="http://stackoverflow.com/questions/264617/when-would-you-prefer-datetime-over-datetimeoffset" target="_blank"&gt;anyone knew when I would rather use &lt;em&gt;DateTime&lt;/em&gt; over &lt;em&gt;DateTimeOffset&lt;/em&gt;&lt;/a&gt;. The responses I got were along the lines of ‘backwards compatibility’ or ‘when you need an abstract time’. My recommendation is that if you haven’t yet looked at the type, go do it now and after that, start using it.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;So what is this &lt;em&gt;DateTimeOffset&lt;/em&gt;?&lt;/strong&gt; When representing a date/time, especially in an internationally-faced system, you have to include a time-zone. &lt;em&gt;DateTime&lt;/em&gt; did a very poor job handling time-zones, like being insensitive to changes. &lt;em&gt;DateTimeOffset&lt;/em&gt; is the exact same thing as &lt;em&gt;DateTime&lt;/em&gt;, only it takes heed of time-zones. For instance, comparing two &lt;em&gt;DateTimeOffset&lt;/em&gt;s with the same UTC time in different time-zones will result in equality.     &lt;br /&gt;Moreover, &lt;em&gt;DateTime&lt;/em&gt; also had only three modes: &lt;em&gt;Local&lt;/em&gt;, &lt;em&gt;UTC&lt;/em&gt; and &lt;em&gt;Unspecified&lt;/em&gt;, whereas &lt;em&gt;DateTimeOffset&lt;/em&gt; has an &lt;em&gt;Offset&lt;/em&gt; property, allowing you to create dates in any time-zone you like.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Things to note:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;font face="Calibri"&gt;&lt;em&gt;DateTime&lt;/em&gt; can be implicitly converted to &lt;em&gt;DateTimeOffset&lt;/em&gt;, but not vice-versa. To do that, you would have to use &lt;em&gt;DateTimeOffset’&lt;/em&gt;s &lt;em&gt;DateTime&lt;/em&gt; property.         &lt;br /&gt;When converting this way, the &lt;em&gt;DateTime’&lt;/em&gt;s kind will always be &lt;em&gt;Unspecified&lt;/em&gt;.&lt;/font&gt;       &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: #2b91af"&gt;DateTimeOffset &lt;/span&gt;dateTimeOffset = &lt;span style="color: #2b91af"&gt;DateTimeOffset&lt;/span&gt;.UtcNow;
&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;dateTime = dateTimeOffset.DateTime;&lt;/code&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;&lt;font face="Calibri"&gt;When parsing a &lt;em&gt;DateTimeOffset&lt;/em&gt;, note that you can specify &lt;em&gt;AssumeUniversal&lt;/em&gt; and &lt;em&gt;AssumeLocal&lt;/em&gt; using the &lt;em&gt;DateTimeStyles&lt;/em&gt; enum. These come in handy when the string you’re parsing has no time-zone data.&lt;/font&gt; 

    &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: #2b91af"&gt;DateTimeOffset&lt;/span&gt;.TryParse(myDateString, &lt;span style="color: #2b91af"&gt;CultureInfo&lt;/span&gt;.InvariantCulture.DateTimeFormat, &lt;span style="color: #2b91af"&gt;DateTimeStyles&lt;/span&gt;.AssumeUniversal, &lt;span style="color: blue"&gt;out &lt;/span&gt;dateTimeOffset))
    dateTimeOffset = &lt;span style="color: blue"&gt;default&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DateTimeOffset&lt;/span&gt;);&lt;/code&gt;&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;&lt;font face="Calibri"&gt;It is a best practice to store all of your dates as UTC in the database, regardless of the physical location of your users / servers. When doing this, be sure to manually change your &lt;em&gt;DateTimeOffset&lt;/em&gt; objects to UTC using &lt;em&gt;ToUniversalTime&lt;/em&gt; and only then use the &lt;em&gt;DateTime&lt;/em&gt; property as I have previously noted. &lt;/font&gt;

    &lt;pre class="code"&gt;&lt;code&gt;SaveTimeToDatabase(dateTimeOffset.ToUniversalTime().DateTime);&lt;/code&gt;&lt;/pre&gt;
Note that you do not need to convert &lt;em&gt;DateTimeOffset&lt;/em&gt; objects to any time-zone to do calculations / comparisons. The only time you need to convert them to a time-zone is for displaying them to the user.&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font face="Calibri"&gt;If you want to store a user’s time-zone (on a database that doesn’t support date/times with time-zones), it would be best to have a translation table and use the &lt;em&gt;TimeZoneInfo&lt;/em&gt; class’s Ids (for instance: &lt;em&gt;TimeZoneInfo.Local.Id&lt;/em&gt;). Then you could use &lt;em&gt;TimeZoneInfo.FindSystemTimeZoneById&lt;/em&gt; to translate that value to a &lt;em&gt;TimeZoneInfo&lt;/em&gt; and use that object’s &lt;em&gt;BaseUtcOffset &lt;/em&gt;property to get the difference from UTC. 

      &lt;br /&gt;This may seem a bit cumbersome, but considering the fact that time-zones change due to daylight savings time, you can’t just store the difference from UTC and would be better suited allowing Windows to take care of these issues for you. 

      &lt;br /&gt;Here’s a sample of this method of conversion:&lt;/font&gt; 

    &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;id = &lt;span style="color: #a31515"&gt;&amp;quot;Israel Standard Time&amp;quot;&lt;/span&gt;;
&lt;span style="color: #2b91af"&gt;DateTimeOffset &lt;/span&gt;utcnow = &lt;span style="color: #2b91af"&gt;DateTimeOffset&lt;/span&gt;.UtcNow;

&lt;span style="color: #2b91af"&gt;DateTimeOffset &lt;/span&gt;now = utcnow.ToOffset(&lt;span style="color: #2b91af"&gt;TimeZoneInfo&lt;/span&gt;.FindSystemTimeZoneById(id).BaseUtcOffset);&lt;/code&gt;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Side note:&lt;/strong&gt; When storing these in the database, it would be prudent to use SQL Server 2008’s &lt;em&gt;datetimeoffset&lt;/em&gt; type, which is the equivalent of &lt;em&gt;DateTimeOffset&lt;/em&gt; and takes care of the time-zones in the same manner.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6795009" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Advices/default.aspx">Advices</category></item><item><title>My View of C# 4.0</title><link>http://weblogs.asp.net/okloeten/archive/2008/10/29/6708812.aspx</link><pubDate>Tue, 28 Oct 2008 23:16:53 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6708812</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>36</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6708812</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/10/29/6708812.aspx#comments</comments><description>&lt;p&gt;I've known a bit about C# 4.0 for a while now and have had time to think about it. I've just re-read the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=csharpfuture&amp;amp;DownloadId=3550" target="_blank"&gt;New features in C# 4.0&lt;/a&gt; paper published by Microsoft and would like to offer the following critique of the language's new features:&lt;/p&gt; &lt;h4&gt;Dynamic Lookup&lt;/h4&gt; &lt;p&gt;&lt;a title="Microsoft PDC 2008 by Manohar Dasari, CC-BY" href="http://flickr.com/photos/manohard/2979001570/"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 5px 5px 5px 10px; border-right-width: 0px" height="181" alt="Microsoft PDC 2008 by Manohar Dasari, CC-BY" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/MyViewofC4.0_6C/1_3.jpg" width="260" align="right" border="0"&gt;&lt;/a&gt;This feature just makes me cringe, just like &lt;a href="http://weblogs.asp.net/okloeten/archive/2003/11/01/35097.aspx" target="_blank"&gt;anonymous methods made me cringe&lt;/a&gt; when they were introduced in C# 2.0. To this day, I hardly use them, as they always feel like a kludge to me (lambda expressions fixed that).&lt;br&gt;The &lt;code&gt;dynamic&lt;/code&gt; keyword is as open to abuse as anything could be. It takes the principles of static typing and throws the baby out with the bathwater.&lt;/p&gt; &lt;h6&gt;&lt;/h6&gt; &lt;h5&gt;What is wrong with it&lt;/h5&gt; &lt;p&gt;When looked at initially, the &lt;code&gt;dynamic&lt;/code&gt; keyword is great, because it simplifies and speeds up what is usually done with Reflection and Primary Interop Assemblies, both in the aspect of development times and the aspect of run time. Unfortunately, too much of a good thing is bad for you. Imagine the following:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public dynamic&lt;/span&gt; GetCustomer()
{
    &lt;span style="color: rgb(0,128,0)"&gt;// mystery...&lt;/span&gt;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What do we have here then? I don't know and neither does IntelliSense? I guess we'll have to go with trial and error.&lt;br&gt;I admit this is quite the dramatization, but you get my point: it's ripe for abusing an otherwise perfectly fine static syntax.&lt;/p&gt;
&lt;p&gt;Moreover, the &lt;code&gt;dynamic&lt;/code&gt; keyword's syntax does what no other feature of C# has ever done - &lt;strong&gt;it breaks existing syntax&lt;/strong&gt;. Should I define in C# 3.0 a type named &lt;code&gt;dynamic&lt;/code&gt;, the following piece of code will take a whole different meaning in C# 4.0:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;dynamic&lt;/span&gt; GetCustomer()
{
    &lt;span style="color: rgb(43,145,175)"&gt;dynamic&lt;/span&gt; customer = GetCustomerCOMObject();
    &lt;span style="color: blue"&gt;return&lt;/span&gt; customer;
}&lt;/code&gt;&lt;/pre&gt;
&lt;h5&gt;How it can be fixed&lt;/h5&gt;
&lt;p&gt;Using the &lt;code&gt;dynamic&lt;/code&gt; keyword is actually a built-in form of &lt;a href="http://weblogs.asp.net/okloeten/archive/2006/10/31/767349.aspx" target="_blank"&gt;Duck Typing&lt;/a&gt;. The idea is good and should be introduced into the language, but I'd like to suggest a different way of doing it:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ICustomer&lt;/span&gt; GetCustomer()
{
    &lt;span style="color: blue"&gt;dynamic&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ICustomer&lt;/span&gt; customer = GetCustomerCOMObject();
    &lt;span style="color: blue"&gt;return&lt;/span&gt; customer;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, what I get back is a dynamic dispatch object that must adhere to a specific interface. This means that &lt;strong&gt;the object graph is checked for conformity&lt;/strong&gt; against &lt;code&gt;ICustomer&lt;/code&gt; &lt;strong&gt;the moment it is cast in the dynamic scope &lt;/strong&gt;(i.e. returned from &lt;code&gt;GetCustomerCOMObject&lt;/code&gt;) and is from this moment on a typed object with dynamic dispatch under the hood. From this moment on, we couldn't care less about whether this object uses dynamic dispatch or not, since we now treat it as a &lt;a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object" target="_blank"&gt;POCO&lt;/a&gt;.&lt;br&gt;This, along with removing of the ability to send dynamic dispatch objects through the call-stack (as parameters and return types), bringing them to the level of anonymous types, will help stop the deterioration of C# into a dynamic language.&lt;/p&gt;
&lt;h4&gt;Named and Optional Arguments&lt;/h4&gt;
&lt;p&gt;&lt;a title="Untitled by Long Zheng, CC-BY-NC-SA" href="http://flickr.com/photos/longzheng/2981755329/"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 5px 5px 5px 10px; border-right-width: 0px" height="181" alt="Untitled by Long Zheng, CC-BY-NC-SA" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/MyViewofC4.0_6C/2_3.jpg" width="260" align="right" border="0"&gt;&lt;/a&gt;This is just silly. Really, this looks like some people cried "we don't like overloads" hard enough and got some VB into the C# the rest of us liked the way it was. If you want to initialize your method with some of the parameters, use a builder pattern with an object initializer instead.&lt;/p&gt;
&lt;p&gt;Here, I'll take the sample at the bottom of page 6 and fix it, C# 3.0 style:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public void&lt;/span&gt; M(&lt;span style="color: blue"&gt;int&lt;/span&gt; x, &lt;span style="color: rgb(43,145,175)"&gt;MBuilder&lt;/span&gt; builder);

&lt;span style="color: blue"&gt;public void&lt;/span&gt; M(&lt;span style="color: blue"&gt;int&lt;/span&gt; x)
{
    &lt;span style="color: blue"&gt;this&lt;/span&gt;.M(x, &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MBuilder&lt;/span&gt;());
}

&lt;span style="color: blue"&gt;public class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MBuilder&lt;/span&gt;
{
    &lt;span style="color: blue"&gt;public&lt;/span&gt; MBuilder() { &lt;span style="color: blue"&gt;this&lt;/span&gt;.Y = 5; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Z = 7; }
    &lt;span style="color: blue"&gt;public int&lt;/span&gt; Y { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public int&lt;/span&gt; Z { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}

M(1, &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MBuilder&lt;/span&gt; { Y = 2, Z = 3 }); &lt;span style="color: rgb(0,128,0)"&gt;// ordinary call of M&lt;/span&gt;
M(1, &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MBuilder&lt;/span&gt; { Y = 2 });        &lt;span style="color: rgb(0,128,0)"&gt;// omitting z – equivalent to M(1, 2, 7)&lt;/span&gt;
M(1);                                &lt;span style="color: rgb(0,128,0)"&gt;// omitting both y and z – equivalent to M(1, 5, 7)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Yes, I do realize it's mainly for COM interop, but most people will just get either confused by all the syntax, abuse it or simply forget it ever existed.&lt;/p&gt;
&lt;h5&gt;What is wrong with it&lt;/h5&gt;
&lt;p&gt;It exists.&lt;/p&gt;
&lt;h5&gt;How it can be fixed&lt;/h5&gt;
&lt;p&gt;Remove it from C#. There - fixed.&lt;br&gt;If you want optional parameters in your COM interop calls, just implement the correct overloads in the interface you create for use with the &lt;code&gt;dynamic&lt;/code&gt; keyword (see my suggestion for dynamic lookups) and the binding will be done at run time by the parameter names.&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;Variance, Covariance and Contravariance&lt;/h4&gt;
&lt;p&gt;These three features are long overdue and &lt;a href="http://weblogs.asp.net/okloeten/archive/2006/10/10/Generic-Type-Variance-in-C_2300_-and-the-CLR.aspx" target="_blank"&gt;finally&lt;/a&gt; make an appearance in the language. It's a great feature and I would love to integrate it into my code as soon as I possibly can.&lt;br&gt;I would love to know if there are plans to not only include reference conversions, but also the implicit and explicit conversion operators as qualifiers for VC&amp;amp;C.&lt;/p&gt;
&lt;h5&gt;What is wrong with it&lt;/h5&gt;
&lt;p&gt;&lt;a title="Anders Heilberg at book signing by DBegley, CC-BY" href="http://flickr.com/photos/begley/2979906130/"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 5px 5px 5px 10px; border-right-width: 0px" height="180" alt="Anders Heilberg at book signing by DBegley, CC-BY" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/MyViewofC4.0_6C/3_3.jpg" width="260" align="right" border="0"&gt;&lt;/a&gt;Although Variance is implicit, the others are explicit. Using the &lt;code&gt;Type&amp;lt;in T&amp;gt;&lt;/code&gt; / &lt;code&gt;Type&amp;lt;out T&amp;gt;&lt;/code&gt; notation is good for being explicit (for instance when you expect your interface to be expanded in the future), but it doesn't have to be and can become a bit annoying over time.&lt;/p&gt;
&lt;h5&gt;How it can be fixed&lt;/h5&gt;
&lt;p&gt;The compiler can very easily infer the fact that your interface is either input-only or output-only and mark it as such for you. Language-wise, the explicit version should be kept available, for when you want to prevent someone (or yourself) from mistakenly adding a new method that breaks the your input / output only design.&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;Summary&lt;/h4&gt;
&lt;p&gt;It looks to me like the team behind C# is going in the wrong direction (DLR) instead of the right direction (&lt;a href="http://weblogs.asp.net/okloeten/archive/2006/10/24/720461.aspx" target="_blank"&gt;Spec#&lt;/a&gt;), slowly turning C# into a dynamic language. It looks like all of this is done for the sake of easy interop with dynamic languages and COM objects. It looks as though the designers have &lt;a href="http://en.wikipedia.org/wiki/Dynamic_Language_Runtime" target="_blank"&gt;succumbed&lt;/a&gt; to &lt;a href="http://www.ironruby.net/" target="_blank"&gt;peer&lt;/a&gt; &lt;a href="http://www.codeplex.com/IronPython/" target="_blank"&gt;pressure&lt;/a&gt;. There are so many features missing from C# and the above are nowhere near the top of my list.&lt;/p&gt;
&lt;p&gt;I can only hope someone is listening.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6708812" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Microsoft Israel's Developers' Academy #3</title><link>http://weblogs.asp.net/okloeten/archive/2008/09/09/6612907.aspx</link><pubDate>Tue, 09 Sep 2008 12:47:28 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6612907</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6612907</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/09/09/6612907.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://www.dooblo.net/ip_wc/Survey.aspx?Ticket=T6K6OGBN" target="_blank"&gt;Voting has been open&lt;/a&gt; for a few days now for which lectures will be featured at the next Microsoft Developers' Academy. The event is the second most important event for Microsoft-platform-developers in Israel, surpassed only by the biennial Tech-Ed Israel.&lt;/p&gt;  &lt;p&gt;Among some very interesting lectures I would love to see, I've suggested a lecture myself. Here's its abstract:&lt;/p&gt;  &lt;blockquote&gt;   &lt;h3&gt;A Deep Dive into LINQ&lt;/h3&gt;    &lt;p&gt;Many developers already use LINQ on a daily basis, but most only scratch the surface of what&amp;#8217;s possible. In this session we will dive more deeply into LINQ and see how it works behind the scenes, share tips, tricks and common pitfalls.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;While most of the lecture's structure is already defined in my head, I'd love to hear from you what topics you would like to see covered, either from the topics covered in my blog's posts or from your own experience.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.dooblo.net/ip_wc/Survey.aspx?Ticket=T6K6OGBN" target="_blank"&gt;Go vote&lt;/a&gt; and I'll be seeing you there! :)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6612907" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Community/default.aspx">Community</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/MSDA/default.aspx">MSDA</category></item><item><title>Visual Studio 2008 Load Testing Checklist</title><link>http://weblogs.asp.net/okloeten/archive/2008/09/09/6612870.aspx</link><pubDate>Tue, 09 Sep 2008 12:33:50 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6612870</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6612870</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/09/09/6612870.aspx#comments</comments><description>&lt;p&gt;&lt;a title="frustrated by Jon Watson, CC-BY-NC-SA" href="http://flickr.com/photos/heatsink/110859301/" target="_blank"&gt;&lt;img style="margin: 5px 5px 5px 10px" alt="frustrated by Jon Watson, CC-BY-NC-SA" src="http://farm1.static.flickr.com/35/110859301_5cc01c7ed5_m.jpg" align="right" border="0" /&gt;&lt;/a&gt; After a &lt;u&gt;couple of days of &lt;strong&gt;trying to run&lt;/strong&gt; a load test &lt;/u&gt;for a web service on several agents via Visual Studio 2008, I come out much wiser and with a few new bald-spots, where hair I pulled out in the process used to be.&lt;/p&gt;  &lt;p&gt;I got a few errors whose messages have nothing to do with what really happened, so here's a quick checklist:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font face="Calibri"&gt;Try restarting the agent service.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Re-register the agent using AgentConfigUtil and then restart the agent service.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Try restarting the controller service.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Are you using a trial version of Visual Studio Team System? If so, it may have expired.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Are all of your agents and controller on the same version?        &lt;br /&gt;Check that the file &lt;em&gt;Microsoft.VisualStudio.QualityTools.LoadTestFramework.dll&lt;/em&gt; is the same version on all of your computers (&lt;a href="http://blogs.msdn.com/billbar/archive/2008/06/13/running-load-tests-with-vsts-2008-sp1-requires-that-sp1-be-installed-on-vs-client-controller-and-agents.aspx" target="_blank"&gt;more information&lt;/a&gt;). Its original version is 9.0.21022.8, SP1's version is 9.0.30729.1. If you installed &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=572E1E71-AE6B-4F92-960D-544CABE62162&amp;amp;displaylang=en" target="_blank"&gt;the trial from Microsoft's site&lt;/a&gt;, your version is pre-SP1.         &lt;br /&gt;If any of the agents differs from the controller, you will not be able to use them and will see them as &lt;strong&gt;Disconnected&lt;/strong&gt; in the &lt;em&gt;Administer Test Controllers&lt;/em&gt; dialog.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Are you using SQL Server as a store for the results?&lt;/font&gt;&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;&lt;font face="Calibri"&gt;Does the user the service runs as have permissions to the SQL Server database?          &lt;br /&gt;You can see which user is used to access the store from Visual Studio's Menu &lt;em&gt;Test -&amp;gt; Administer Test Controllers&lt;/em&gt;.&lt;/font&gt; &lt;/li&gt;      &lt;li&gt;&lt;font face="Calibri"&gt;If you're getting login errors, check that the SQL Server allows remote connections. If it does, consider using a SQL Server user instead of Windows Authentication. Remember to make sure the SQL Server allows both types of connections (Mixed Mode) before attempting this.&lt;/font&gt; &lt;/li&gt;      &lt;li&gt;&lt;font face="Calibri"&gt;Make sure the computer from which the SQL Server is running &lt;a href="http://msdn.microsoft.com/en-us/library/ms175043.aspx" target="_blank"&gt;has an exception&lt;/a&gt; for incoming connections on TCP port 1443.&lt;/font&gt; &lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Using the trial version of the agents? Getting &lt;em&gt;&amp;quot;The Visual Studio Team Test Load Agent license has expired&amp;quot;&lt;/em&gt; way before your 90 day trial period has elapsed? &lt;/font&gt;&lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2641701&amp;amp;SiteID=1" target="_blank"&gt;&lt;font face="Calibri"&gt;You've probably hit the 25 tests mark&lt;/font&gt;&lt;/a&gt;&lt;font face="Calibri"&gt; (note that the tests are per-CPU, rather than per-computer).        &lt;br /&gt;You can work around this if you change the user running the service. &lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Around that time I decided I've had enough with all this trial stuff. I was thinking about purchasing licenses, but then I took a look and found that you can only &lt;a href="http://msdn.microsoft.com/en-us/vsts2008/products/cc263908.aspx" target="_blank"&gt;use load agents when you've got a Volume License&lt;/a&gt;!&lt;/p&gt;  &lt;p&gt;At that point, I went and just ran the test on a few computers at once.&lt;/p&gt;  &lt;ul&gt;&lt;/ul&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6612870" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Toys/default.aspx">Toys</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Advices/default.aspx">Advices</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>Extending The Partial Classes of Stored Procedures' Results in LINQ to SQL</title><link>http://weblogs.asp.net/okloeten/archive/2008/09/04/6598448.aspx</link><pubDate>Thu, 04 Sep 2008 09:31:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6598448</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6598448</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/09/04/6598448.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/ExtendingThePartialClassesofStoredProced_A97D/image_2.png" mce_href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/ExtendingThePartialClassesofStoredProced_A97D/image_2.png"&gt;&lt;img src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/ExtendingThePartialClassesofStoredProced_A97D/image_thumb.png" style="border-width: 0px; margin: 5px 5px 5px 10px;" alt="Original: Where do we go next? by Anke L, CC-BY-NC-SA" mce_src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/ExtendingThePartialClassesofStoredProced_A97D/image_thumb.png" align="right" border="0" width="244" height="184"&gt;&lt;/a&gt; One of the nice things about LINQ to SQL is the ability to extend the types of the generated entities. Another nice thing is being able to get typed results from stored procedures. Let's try and combine the two together.&lt;/p&gt;  &lt;p&gt;We'll take the following generated code for instance:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public partial class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;GetStuffResult&lt;br&gt;&lt;/span&gt;{&lt;br&gt;    &lt;br&gt;    &lt;span style="color: blue;"&gt;private int &lt;/span&gt;_Id;&lt;br&gt;    &lt;br&gt;    &lt;span style="color: blue;"&gt;private string &lt;/span&gt;_Name;&lt;br&gt;    &lt;br&gt;    &lt;span style="color: blue;"&gt;public &lt;/span&gt;GetStuffResult()&lt;br&gt;    {&lt;br&gt;    }&lt;br&gt;    &lt;br&gt;    [&lt;span style="color: rgb(43, 145, 175);"&gt;Column&lt;/span&gt;(Storage=&lt;span style="color: rgb(163, 21, 21);"&gt;"_Id"&lt;/span&gt;, DbType=&lt;span style="color: rgb(163, 21, 21);"&gt;"INT NOT NULL"&lt;/span&gt;)]&lt;br&gt;    &lt;span style="color: blue;"&gt;public int &lt;font color="#000000"&gt;Id&lt;/font&gt;&lt;/span&gt;&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;get&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;return this&lt;/span&gt;._Id;&lt;br&gt;        }&lt;br&gt;        &lt;span style="color: blue;"&gt;set&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;if &lt;/span&gt;((&lt;span style="color: blue;"&gt;this&lt;/span&gt;._Id != &lt;span style="color: blue;"&gt;value&lt;/span&gt;))&lt;br&gt;            {&lt;br&gt;                &lt;span style="color: blue;"&gt;this&lt;/span&gt;._Id = &lt;span style="color: blue;"&gt;value&lt;/span&gt;;&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;    &lt;br&gt;    [&lt;span style="color: rgb(43, 145, 175);"&gt;Column&lt;/span&gt;(Storage=&lt;span style="color: rgb(163, 21, 21);"&gt;"_Name"&lt;/span&gt;, DbType=&lt;span style="color: rgb(163, 21, 21);"&gt;"NVarChar(100)"&lt;/span&gt;)]&lt;br&gt;    &lt;span style="color: blue;"&gt;public string &lt;font color="#000000"&gt;Name&lt;/font&gt;&lt;/span&gt;&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;get&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;return this&lt;/span&gt;._Name;&lt;br&gt;        }&lt;br&gt;        &lt;span style="color: blue;"&gt;set&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;if &lt;/span&gt;((&lt;span style="color: blue;"&gt;this&lt;/span&gt;._Name != &lt;span style="color: blue;"&gt;value&lt;/span&gt;))&lt;br&gt;            {&lt;br&gt;                &lt;span style="color: blue;"&gt;this&lt;/span&gt;._Name = &lt;span style="color: blue;"&gt;value&lt;/span&gt;;&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I would like to extend it by adding a new property to it, named TranslatedId, which translates the returned Id to and from a Guid:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;partial class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;GetStuffResult&lt;br&gt;&lt;/span&gt;{&lt;br&gt;&lt;span style="color: blue;"&gt;    public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Guid &lt;/span&gt;TranslatedId&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;get&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Translator&lt;/span&gt;.GetGuid(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Id);&lt;br&gt;        }&lt;br&gt;        &lt;span style="color: blue;"&gt;set&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.ProjectId = &lt;span style="color: rgb(43, 145, 175);"&gt;Translator&lt;/span&gt;.GetInt(&lt;span style="color: blue;"&gt;value&lt;/span&gt;);&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Translator's two methods translate between the integer id and the Guid id. Note that 0 translates to and from an empty Guid.&lt;/p&gt;

&lt;p&gt;I'll try to run it and will find out that &lt;b&gt;all of my Ids are 0&lt;/b&gt;. Why is that?&lt;/p&gt;

&lt;p&gt;Apparently, when running a stored procedure in LINQ to SQL, it requires all of the properties defined on the class to have both getters and setters (which means &lt;b&gt;you can not write read-only properties in the partial class&lt;/b&gt;) and sets all non-result (i.e. non-generated) properties to their default values. This means that although the real Id gets selected from the database, TranslatedId gets set to an empty Guid immediately afterwards, which in turn overrides Id to 0.&lt;/p&gt;

&lt;p&gt;To work around this, we have to apply a dirty little hack:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;partial class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;GetStuffResult&lt;br&gt;&lt;/span&gt;{&lt;br&gt;&lt;i&gt;    &lt;span style="color: blue;"&gt;private &lt;span style="color: rgb(43, 145, 175);"&gt;Guid &lt;/span&gt;&lt;/span&gt;dupe;&lt;br&gt;&lt;br&gt;    [&lt;span style="color: rgb(43, 145, 175);"&gt;Column&lt;/span&gt;(Storage = &lt;span style="color: rgb(163, 21, 21);"&gt;"dupe"&lt;/span&gt;)]&lt;br&gt;&lt;/i&gt;&lt;span style="color: blue;"&gt;    public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Guid &lt;/span&gt;TranslatedId&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;get&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Translator&lt;/span&gt;.GetGuid(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Id);&lt;br&gt;        }&lt;br&gt;        &lt;span style="color: blue;"&gt;set&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.ProjectId = &lt;span style="color: rgb(43, 145, 175);"&gt;Translator&lt;/span&gt;.GetInt(&lt;span style="color: blue;"&gt;value&lt;/span&gt;);&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is pretty horrible, but what it does is direct LINQ to SQL to set the default value into the property's storage field, which is some dupe field, instead of into the property itself. Now the field's value will be reset and not the property's. Note that the dupe field's type should be the same as the property's type.&lt;/p&gt;

&lt;p&gt;Yes, it's ugly, but this is the only workaround I found. If anyone knows of any other workaround or solution to this problem, I'd love to hear it.&lt;/p&gt;

&lt;p&gt;[Original image used: &lt;a href="http://flickr.com/photos/13965522@N00/2697534121/" mce_href="http://flickr.com/photos/13965522@N00/2697534121/" target="_blank"&gt;Where do we go next?&lt;/a&gt;]&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6598448" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Linq+to+SQL/default.aspx">Linq to SQL</category></item><item><title>Announcing New C# 4.0 LINQ Features and Book</title><link>http://weblogs.asp.net/okloeten/archive/2008/08/29/6575250.aspx</link><pubDate>Fri, 29 Aug 2008 10:12:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6575250</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6575250</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/08/29/6575250.aspx#comments</comments><description>&lt;p&gt;&lt;b&gt;* &lt;a href="http://weblogs.asp.net/fmarguerie/" mce_href="http://weblogs.asp.net/fmarguerie/"&gt;Fabrice Marguerie&lt;/a&gt;, co-author of &lt;a href="http://linqinaction.net/" mce_href="http://linqinaction.net/"&gt;LINQ In Action&lt;/a&gt;, asked me to clarify that this post has absolutely nothing to do with their book and is simply meant in jest &lt;/b&gt;*&lt;br&gt;&lt;/p&gt;&lt;p&gt;LINQ has been around for quite a while, making our lives easier with its short, declarative syntax. During talks in closed sessions, the persons behind C# have come up with some great plans for its next version, including some vast improvements to LINQ. The team wanted to make everyone's lives even better by introducing some ground-breaking features to it.&lt;/p&gt; &lt;p&gt;I'd like to introduce, out of those finalized or at the final draft stage, some of the new keywords that have been introduced to the language with regards to LINQ and by doing so, expose the reader to this new, exciting technology.&lt;/p&gt; &lt;h5&gt;&lt;/h5&gt; &lt;h4&gt;fine&lt;/h4&gt; &lt;p&gt;Many of the lines of code incorporated into LINQ are about variable validation. How many times have you had to write pieces of code like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; values = &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; numbers&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;where&lt;/span&gt; n &amp;gt; 0 &amp;amp;&amp;amp; n &amp;lt; 10&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt; n;&lt;/pre&gt;
&lt;p&gt;This piece of code is very trivial and the compiler is smart enough to understand it on its own. From C# 4.0, the &lt;code&gt;fine&lt;/code&gt; keyword is introduced and the above query will be replaced with:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; values = &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; numbers&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;where&lt;/span&gt; n &lt;span style="color: rgb(0, 0, 255);"&gt;is fine&lt;/span&gt;&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt; n;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;&lt;/h1&gt;
&lt;h4&gt;some&lt;/h4&gt;
&lt;p&gt;Most LINQ queries do not iterate over an entire enumeration. Some filter out results, some use &lt;code&gt;Take&lt;/code&gt;, &lt;code&gt;Skip&lt;/code&gt; and other methods in order to run on only a subset of the elements. Since this is an &lt;b&gt;extremely&lt;/b&gt; common scenario, the team has decided to integrate it into the language using the new &lt;code&gt;some&lt;/code&gt; keyword:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; values = &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0, 0, 255);"&gt;in some&lt;/span&gt; numbers&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt; n;&lt;/pre&gt;
&lt;p&gt;This is complemented with the new &lt;code&gt;most&lt;/code&gt; keyword, which is like &lt;code&gt;some&lt;/code&gt;, only it takes more elements.&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;somewhere&lt;/h4&gt;
&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_8.png" mce_href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_8.png"&gt;&lt;img src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_thumb_3.png" style="border-width: 0px; margin: 0px 10px 0px 5px;" alt="Global Warming vs. LINQ from Clauses" mce_src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_thumb_3.png" width="244" align="right" border="0" height="148"&gt;&lt;/a&gt; The &lt;code&gt;from&lt;/code&gt; clause is known to all who use LINQ, as it indicates the source of the data. Since this clause is a recurring pattern (statistical surveys have shown it to occur 99% of the time, allowing for a 4% standard deviation), the team behind the new language features has decided to cut it out, thus removing unnecessary verbosity from the language. This, in turn, will cause less characters to be written and rewritten, meaning less keystrokes, less keyboard wear, less purchases of new keyboards, leading to prevention of global warming, a goal near to Microsoft's heart. You can see Microsoft's predictions for this in the graph to the right.&lt;/p&gt;
&lt;p&gt;This is possible due to the new &lt;code&gt;somewhere&lt;/code&gt; keyword, as described below: &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; values = &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;somewhere&lt;/span&gt;&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt; n;&lt;/pre&gt;
&lt;p&gt;This feature relies heavily upon the newly introduced Type Inference Transfer System (abbreviated MANTA), a technology recently demonstrated by Microsoft Research.&lt;/p&gt;
&lt;h4&gt;The Book&lt;/h4&gt;
&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_2.png" mce_href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_2.png"&gt;&lt;img src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_thumb.png" style="border-width: 0px; margin: 0px 5px 0px 10px;" alt="LINQ Inaction" mce_src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_thumb.png" width="164" align="right" border="0" height="204"&gt;&lt;/a&gt;I've long since wanted to write a book about something. Since this is just the tip of the iceberg, I've decided to write my book about this subject. It will contain most of what I know (hey, I still need to speak about something at conferences &lt;b&gt;and&lt;/b&gt; release a second edition some day, right?) about the new language. I think that with all of the new features, making our lives as developers that much easier, I've picked an appropriate title.&lt;/p&gt;
&lt;p&gt;Here's an excerpt from the introduction:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Recent advances in compilation technologies have brought with them less need for verbosity. In its stead come smart compilers, which infer much of the work for the developer. With such technology, there's not a lot you, as a developer, need to do anymore. You can simply write a short statement, validate its correctness, check the code in and go home for the day.&lt;/p&gt;
&lt;p&gt;Welcome to the world of tomorrow. Put your feet up.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Here's the layout of the book, as I plan it:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Introduction 
&lt;/li&gt;&lt;li&gt;New Features in LINQ&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;What else would you like to see in the book? Let me know and I might just send you a copy of the rough draft for review once I finish it.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6575250" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Humor/default.aspx">Humor</category></item><item><title>Throw Before You Yield</title><link>http://weblogs.asp.net/okloeten/archive/2008/08/25/6564322.aspx</link><pubDate>Mon, 25 Aug 2008 19:23:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6564322</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6564322</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/08/25/6564322.aspx#comments</comments><description>&lt;p&gt;In &lt;a href="http://weblogs.asp.net/okloeten/archive/2008/08/01/6463327.aspx#6558422" mce_href="http://weblogs.asp.net/okloeten/archive/2008/08/01/6463327.aspx#6558422" target="_blank"&gt;a comment left&lt;/a&gt; by &lt;a href="http://blogs.bartdesmet.net/bart" mce_href="http://blogs.bartdesmet.net/bart" target="_blank"&gt;Bart De Smet&lt;/a&gt;, he pointed out that I failed to address the fact that the execution of all "yielding" methods is deferred.&lt;/p&gt; &lt;p&gt;For instance, when running the following code, no exceptions will be thrown:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] arr = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; copy = arr.Enumerate();&lt;br&gt;&lt;br&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br&gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// ...&lt;br&gt;&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Enumerate&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;// Check to see that enumeration is not null&lt;br&gt;&lt;/span&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (enumeration == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"enumeration"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; enumeration)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; item;&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is because the first time the code from the method will run will be after there a call to &lt;code&gt;copy&lt;/code&gt;'s &lt;code&gt;GetEnumerator()&lt;/code&gt;, followed by a call to that object's &lt;code&gt;MoveNext()&lt;/code&gt; method has been made (i.e. when we enumerate over &lt;code&gt;copy&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;To overcome this problem, we'll change the code slightly:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Enumerate&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;// Check to see that enumeration is not null&lt;br&gt;&lt;/span&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (enumeration == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"enumeration"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; EnumerateCore(enumeration);&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; EnumerateCore&amp;lt;T&amp;gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; enumeration)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; item;&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now a &lt;code&gt;NullReferenceException&lt;/code&gt; will immediately be thrown out of &lt;code&gt;Enumerate&lt;/code&gt;, since the "unyielding" method will first run the code and then defer-call to the "yielding" method. This helps our "yielding" methods adhere to the principal of &lt;a href="http://en.wikipedia.org/wiki/Fail-fast" mce_href="http://en.wikipedia.org/wiki/Fail-fast" target="_blank"&gt;fail-fast&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6564322" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx">C#</category></item></channel></rss>