<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>.NET (C#)</title>
        <link>http://geekswithblogs.net/sdorman/category/4833.aspx</link>
        <description>.NET topics covering C# only.
</description>
        <language>en-US</language>
        <copyright>Scott Dorman</copyright>
        <managingEditor>sj_dorman@hotmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
        <item>
            <title>Sams Teach Yourself Visual C# 2010 in 24 Hours</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/11/05/sams-teach-yourself-visual-c-2010-in-24-hours.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/SamsTeachYourselfVisualC2010in24Hours_143D1/0672331012_4.jpg" target="_blank"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="0672331012" border="0" alt="0672331012" align="left" src="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/SamsTeachYourselfVisualC2010in24Hours_143D1/0672331012_thumb_1.jpg" width="188" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I have &lt;a href="http://geekswithblogs.net/sdorman/archive/2009/06/26/sams-teach-yourself-c-2010-in-24-hours.aspx"&gt;previously mentioned&lt;/a&gt; that I’m working on the upcoming &lt;u&gt;Sams Teach Yourself Visual C# 2010 in 24 Hours&lt;/u&gt; book.&lt;/p&gt;  &lt;p&gt;Things are progressing nicely, if a bit slower than I’d prefer. I have the draft for the cover art, which looks really good.&lt;/p&gt;  &lt;p&gt;I’m also very pleased to announce my technical editors – Claudio Lasaala and Eric Lippert.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://claudiolassala.spaces.live.com" target="_blank"&gt;Claudio&lt;/a&gt; is a Senior Developer at &lt;a href="http://eps-software.com/" target="_blank"&gt;EPS Software Corp&lt;/a&gt;. He has presented several lectures at Microsoft events such as PDC and various other Microsoft seminars, as well as several conferences and user groups across North America and Brazil. He is a multiple winner of the Microsoft MVP Award and also holds the MCSD for .NET certification. He has articles published on several magazines, such as MSDN Brazil Magazine, CoDe Magazine, UTMag, Developers Magazine, and FoxPro Advisor.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/ericlippert" target="_blank"&gt;Eric&lt;/a&gt; is a senior software design engineer at Microsoft has been working full time in the developer division since 1996, where he assisted with the design and implementation of VBScript, JScript, JScript.NET, Windows Script Host, Visual Studio Tools for Office and C#. &lt;/p&gt;  &lt;p&gt;I am looking forward to working with both Claudio and Eric through the technical editing process and getting this book completed and available, which should happen sometime early in 2010.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e2c1b31f-005e-4de6-99b5-352723dad9be" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Books" rel="tag"&gt;Books&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Sams+Teach+Yourself+C%23+2010" rel="tag"&gt;Sams Teach Yourself C# 2010&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136051"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136051" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/136051.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/11/05/sams-teach-yourself-visual-c-2010-in-24-hours.aspx</guid>
            <pubDate>Fri, 06 Nov 2009 04:01:43 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/136051.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/11/05/sams-teach-yourself-visual-c-2010-in-24-hours.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/136051.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/136051.aspx</trackback:ping>
        </item>
        <item>
            <title>Visual Studio 2010 Beta 2</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/10/19/visual-studio-2010-beta-2.aspx</link>
            <description>&lt;p&gt;Visual Studio 2010 Beta 2 is now available for &lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;MSDN subscribers&lt;/a&gt;, and generally available on October 21. I haven’t had a chance yet to play around with it, but some of the CLR changes that are &lt;strong&gt;very&lt;/strong&gt; exciting to hear about are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The new &lt;a href="http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(VS.100).aspx" target="_blank"&gt;String.IsNullOrWhiteSpace&lt;/a&gt; method indicates whether a string is null, empty, or consists only of white-space characters. &lt;/li&gt;    &lt;li&gt;New overloads have been added to the &lt;a href="http://msdn.microsoft.com/en-us/library/system.string.concat(VS.100).aspx" target="_blank"&gt;String.Concat&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.string.join(VS.100).aspx" target="_blank"&gt;String.Join&lt;/a&gt; methods that concatenate members of an &lt;a href="http://msdn.microsoft.com/en-us/library/9eekhta0(VS.100).aspx" target="_blank"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt; collections.&lt;/li&gt;    &lt;li&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/dd991828(VS.100).aspx" target="_blank"&gt;String.Concat&amp;lt;T&amp;gt;&lt;/a&gt; method lets you concatenate each element in an enumerable collection without first converting the elements to strings.&lt;/li&gt;    &lt;li&gt;The new &lt;a href="http://msdn.microsoft.com/en-us/library/system.enum.hasflag(VS.100).aspx" target="_blank"&gt;Enum.HasFlag&lt;/a&gt; method determines whether one or more bit fields or flags are set in an enumeration value. &lt;/li&gt;    &lt;li&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/dd991876(VS.100).aspx" target="_blank"&gt;Enum.TryParse&amp;lt;TEnum&amp;gt;&lt;/a&gt; method returns a Boolean value that indicates whether a string or integer value could be successfully parsed.&lt;/li&gt;    &lt;li&gt;You can now easily copy one stream into another with the &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.stream.copyto(VS.100).aspx" target="_blank"&gt;CopyTo&lt;/a&gt; method in classes that inherit from the &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.stream(VS.100).aspx" target="_blank"&gt;System.IO.Stream&lt;/a&gt; class.&lt;/li&gt;    &lt;li&gt;New &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.path.combine(VS.100).aspx" target="_blank"&gt;Path.Combine&lt;/a&gt; method overloads enable you to combine file paths.&lt;/li&gt;    &lt;li&gt;You can now parse &lt;a href="http://msdn.microsoft.com/en-us/library/system.guid(VS.100).aspx" target="_blank"&gt;System.Guid&lt;/a&gt; structures. &lt;/li&gt;    &lt;li&gt;The new &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.registryoptions(VS.100).aspx" target="_blank"&gt;Microsoft.Win32.RegistryOptions&lt;/a&gt; enumeration lets you specify a volatile registry key that does not persist after the computer restarts.&lt;/li&gt;    &lt;li&gt;Registry keys no longer are restricted to a maximum length of 255 characters.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I’m really looking forward to these improvements, particularly &lt;a href="http://msdn.microsoft.com/en-us/library/system.enum.hasflag(VS.100).aspx" target="_blank"&gt;Enum.HasFlags&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/dd991876(VS.100).aspx" target="_blank"&gt;Enum.TryParse&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(VS.100).aspx" target="_blank"&gt;String.IsNullOrWhiteSpace&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/system.guid.tryparse(VS.100).aspx" target="_blank"&gt;Guid.TryParse&lt;/a&gt;, and &lt;a href="http://msdn.microsoft.com/en-us/library/dd991142(VS.100).aspx" target="_blank"&gt;Path.Combine(string[])&lt;/a&gt;. Of course, since some of these weren’t in Beta 1 (at least as far as I saw), it means revising some of the chapters for my &lt;a href="http://geekswithblogs.net/sdorman/archive/2009/06/26/sams-teach-yourself-c-2010-in-24-hours.aspx" target="_blank"&gt;book&lt;/a&gt;.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a70bc1ed-edf2-4fb9-846e-4a250de745b7" class="wlWriterSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Visual+Studio+2010" rel="tag"&gt;Visual Studio 2010&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Beta+2" rel="tag"&gt;Beta 2&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET+4.0" rel="tag"&gt;.NET 4.0&lt;/a&gt;&lt;/div&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right; margin:0px; padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fgeekswithblogs.net%2fsdorman%2farchive%2f2009%2f10%2f19%2fvisual-studio-2010-beta-2.aspx&amp;amp;title=Visual+Studio+2010+Beta+2"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border: 0" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135560"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135560" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/135560.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/10/19/visual-studio-2010-beta-2.aspx</guid>
            <pubDate>Mon, 19 Oct 2009 18:09:29 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/135560.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/10/19/visual-studio-2010-beta-2.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/135560.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/135560.aspx</trackback:ping>
        </item>
        <item>
            <title>Premature optimization is the root of all evil</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/08/28/premature-optimization-is-the-root-of-all-evil.aspx</link>
            <description>&lt;p&gt;I’ve lost count how many times I’ve heard that quote. It’s a great quote said by one of the leading minds in computer programming and formal mathematics, &lt;a href="http://en.wikipedia.org/wiki/Donald_Knuth" target="_blank"&gt;Donald Knuth&lt;/a&gt;. The problem with this quote is that, like so many other things, people have only remembered (or only quote) a small portion of the entire thing.&lt;/p&gt;  &lt;p&gt;To read the full quote, you need to look at a paper published in December 1974 titled &lt;u&gt;Structured Programming with go to Statements&lt;/u&gt; in Computing Surveys.&lt;/p&gt;  &lt;p&gt;The full quote is this (in reference to a loop optimization technique that provides a 12% performance improvement):&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The conventional wisdom shared by many of today's software engineers calls for ignoring efficiency in the small; but I believe this is simply an overreaction to the abuses they see being practiced by pennywise-     &lt;br /&gt;and-pound-foolish programmers, who can't debug or maintain their "optimized" programs. In established engineering disciplines a 12% improvement, easily obtained, is never considered marginal; and I believe the same viewpoint should prevail in software engineering. Of course I wouldn't bother making such optimizations on a oneshot job, but when it's a question of preparing quality programs, I don't want to restrict myself to tools that deny me such efficiencies.&lt;/p&gt;    &lt;p&gt;There is no doubt that the grail of efficiency leads to abuse. Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.&lt;/p&gt;    &lt;p&gt;Yet we should not pass up our opportunities in that critical 3%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code     &lt;br /&gt;has been identified. It is often a mistake to make a priori judgments about what parts of a program are really critical, since the universal experience of programmers who have been using measurement tools has been      &lt;br /&gt;that their intuitive guesses fail. &lt;font size="1"&gt;(Computing Surveys, Vol. 6, No. 4, December 1974, p. 268 [p. 8 of the &lt;a href="http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf" target="_blank"&gt;PDF&lt;/a&gt;])&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;While the overall sentiment could be summed up simply as “premature optimization is the root of all evil”, I think the point being made here is that &lt;strong&gt;well-known optimizations&lt;/strong&gt; can and should be applied otherwise time spent on optimizing code should be focused on that small percent of critical code and should be performed with quantitative means.&lt;/p&gt;  &lt;p&gt;What are well-known optimizations? That partly depends on your programming language and partly on your problem domain. Optimizations specific to a problem domain are very domain specific and not easily communicated outside of that domain. However, language specific well-known optimizations are a different story.&lt;/p&gt;  &lt;p&gt;Take string concatenation inside a loop, particularly for languages like C# where strings are immutable objects. Why is that important? Since strings are immutable, they cannot be changed once created. When concatenating two strings you incur overhead from the creation of temporary strings. For example, consider the following code:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;string s = "this is " + "a test.";&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;While this is an arbitrary example, in order for this line to execute it would be necessary to create 3 strings. (Yes, I know the compiler (and if not the compiler, the JIT) would optimize this to remove the concatenation, but pretend for a moment that it didn’t.) Now, imagine this line running inside a loop 10 times. That loop has just created approximately 30 temporary string objects to perform that concatenation. In such a situation, it should be intuitive that the use of a StringBuilder to perform the string concatenation is preferable as you no longer have the intermediate string object creation and result in one allocation to create the StringBuilder and one allocation to convert the StringBuilder to an actual string object. There is always the possibility that while iterating through the loop the StringBuilder may need to perform a few additional buffer reallocations, but that still results in less overhead than creating immutable string objects.&lt;/p&gt;

&lt;p&gt;The point here is that while it is true that premature optimization is the root of all evil, optimization should be a measured undertaking and well-known optimizations applied early are not premature.&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://geekswithblogs.net/sdorman/archive/2009/08/28/premature-optimization-is-the-root-of-all-evil.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://geekswithblogs.net/sdorman/archive/2009/08/28/premature-optimization-is-the-root-of-all-evil.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134372"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134372" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/134372.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/08/28/premature-optimization-is-the-root-of-all-evil.aspx</guid>
            <pubDate>Sat, 29 Aug 2009 03:24:41 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/134372.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/08/28/premature-optimization-is-the-root-of-all-evil.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/134372.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/134372.aspx</trackback:ping>
        </item>
        <item>
            <title>Getting a custom attribute</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/06/08/getting-a-custom-attribute.aspx</link>
            <description>&lt;div id="codeSnippetWrapper"&gt;Attribute programming has a lot of benefits and, when done correctly, can greatly simplify the amount of code that you need to write. One drawback to using attributes is that the code required to retrieve a custom attribute from a type is a bit cumbersome and is very repetitious.&lt;/div&gt;  &lt;p&gt;Given a type, the simplest way to retrieve a custom attribute is code like&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;CustomAttribute attribute = Attribute.GetCustomAttribute(customType.GetType(), &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(CustomAttribute), &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;) &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; CustomAttribute;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;While this is simple code, it doesn’t handle any error conditions and requires that you always remember to perform the cast. A more complete method would look like&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; height: 275px; max-height: 400px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; CustomAttribute GetAttribute(MemberInfo element)&lt;br /&gt;{&lt;br /&gt;    CustomAttribute attribute = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;&lt;br /&gt;    {&lt;br /&gt;        attribute = Attribute.GetCustomAttribute(element, &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(CustomAttribute), &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;) &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; CustomAttribute;&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #008000"&gt;// We aren't really interested in the exceptions here, but if we do get an exception&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000"&gt;// just return null;&lt;/span&gt;&lt;br /&gt;        attribute = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; attribute;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div&gt;This nicely encapsulates the error handling and casting, but introduces another drawback. In order to make use of this method you would need to include it on every custom attribute you create, being sure to change the types appropriately.&lt;/div&gt;

&lt;div&gt; &lt;/div&gt;

&lt;div&gt;We can make this more practical by changing to a generic extension method with very little effort&lt;/div&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; height: 336px; max-height: 400px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; T GetAttribute&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; MemberInfo element) &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; T: Attribute&lt;br /&gt;{&lt;br /&gt;    T attribute = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (element != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;&lt;br /&gt;        {&lt;br /&gt;            attribute = Attribute.GetCustomAttribute(element, &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T), &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;) &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; T;&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt;&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #008000"&gt;// We aren't really interested in the exceptions here, but if we do get an exception&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000"&gt;// just return null;&lt;/span&gt;&lt;br /&gt;            attribute = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; attribute;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div&gt;The benefit here is that, because this is implemented as an extension method it is available as if it were a real method call on any class derived from MemberInfo, which happens to be the base class for all of the Type classes.&lt;/div&gt;

&lt;div&gt; &lt;/div&gt;

&lt;div&gt;Now, we can define our custom attributes without any special consideration to providing a strongly typed GetAttribute method and when we want to retrieve a custom attribute, we can use code that now looks like&lt;/div&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;CustomAttribute attribute = customType.GetType().GetAttribute&amp;lt;CustomAttribute&amp;gt;();&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div&gt;It might not look like a major change in the calling site, but we are now able to quickly and easily get a strongly typed attribute given an instance type. 
  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:271cffa5-0f80-4f36-b0e8-4e4f9d76b7a6" class="wlWriterSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Attributes" rel="tag"&gt;Attributes&lt;/a&gt;&lt;/div&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right; margin:0px; padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fgeekswithblogs.net%2fsdorman%2farchive%2f2009%2f06%2f08%2fgetting-a-custom-attribute.aspx&amp;amp;title=Getting+a+custom+attribute"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border: 0" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=132691"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=132691" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/132691.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/06/08/getting-a-custom-attribute.aspx</guid>
            <pubDate>Mon, 08 Jun 2009 18:31:58 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/132691.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/06/08/getting-a-custom-attribute.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/132691.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/132691.aspx</trackback:ping>
        </item>
        <item>
            <title>Code Contracts in .NET</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/03/02/code-contracts-in-.net.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/CodeContractsin.NET_10367/dd491992_codecontracts_project(en-us)_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="dd491992_codecontracts_project(en-us)" border="0" alt="dd491992_codecontracts_project(en-us)" src="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/CodeContractsin.NET_10367/dd491992_codecontracts_project(en-us)_thumb.png" width="443" height="169" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I have &lt;a target="_blank" href="http://geekswithblogs.net/sdorman/archive/2008/12/10/more-on-.net-4.0-code-contracts.aspx"&gt;previously&lt;/a&gt; talked about code contracts in .NET 4.0, and while .NET 4.0 isn’t out yet you can work with them now in Visual Studio 2008 through &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx"&gt;MSDN DevLabs&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;In case you aren’t familiar with code contracts in .NET, this is a feature that was actually built by the .NET CLR team to provide a language-agnostic way to express code assumptions in the form of pre-conditions, post-conditions, and object invariants.&lt;/p&gt;  &lt;p&gt;There are currently two tools provided:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Runtime Checking&lt;/strong&gt;, which uses a binary rewriter to modify a program by injecting the contracts, which are checked as part of program execution.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Static Checking &lt;/strong&gt;can decide if there are any contract violations without even running the program. It checks for implicit contracts, such as null dereferences and array bounds, as well as the explicit contracts. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This is definitely something to take a look at and start getting familiar with in order to simplify your code and testing.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:3fb497fb-909c-4674-a27d-5ae1a6ce58c2" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Code+Contracts" rel="tag"&gt;Code Contracts&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129801"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129801" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/129801.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/03/02/code-contracts-in-.net.aspx</guid>
            <pubDate>Tue, 03 Mar 2009 00:44:15 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/129801.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/03/02/code-contracts-in-.net.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/129801.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/129801.aspx</trackback:ping>
        </item>
        <item>
            <title>MVP Summit 2009 Twitters</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/03/01/mvp-summit-2009-twitters.aspx</link>
            <description>&lt;p&gt;If you’re at MVP Summit 2009 (or not at Summit) and trying to find people to follow I’ve started putting together a list of MVPs and Microsoft employees that I know who are here.&lt;/p&gt;  &lt;p&gt;Everyone &lt;em&gt;should&lt;/em&gt; be using the #mvp09 hashtag for their tweets, so you can follow the conversations using &lt;a target="_blank" href="http://hashtags.org/tag/mvp09"&gt;#hashtags&lt;/a&gt;, &lt;a target="_blank" href="http://search.twitter.com/search?q=%23mvp09"&gt;Twitter Search&lt;/a&gt;, &lt;a target="_blank" href="http://twopular.com/trend/_mvp09"&gt;Twopular&lt;/a&gt;, &lt;a target="_blank" href="http://tweetscan.com/index.php?s=%23mvp09"&gt;TweetScan&lt;/a&gt;, or any other twitter search sites.&lt;/p&gt;  &lt;p&gt;If you want to be included in this list (or have updates to your information), leave a comment or message me on Twitter.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h1&gt;&lt;strong&gt;Microsoft MVPs&lt;/strong&gt;&lt;/h1&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="1101"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="147"&gt;&lt;strong&gt;&lt;u&gt;Name&lt;/u&gt;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;strong&gt;&lt;u&gt;Twitter&lt;/u&gt;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;strong&gt;&lt;u&gt;Blog&lt;/u&gt;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;&lt;strong&gt;&lt;u&gt;Location&lt;/u&gt;&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Scott Dorman&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/sdorman"&gt;http://twitter.com/sdorman&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a target="_blank" href="http://geekswithblogs.net/sdorman"&gt;http://geekswithblogs.net/sdorman&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Tampa, FL&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Nikita Polyakov&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/NikitaP"&gt;http://twitter.com/NikitaP&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="http://geekswithblogs.net/campuskoder/Default.aspx" target="_blank" href="http://geekswithblogs.net/campuskoder/Default.aspx"&gt;http://geekswithblogs.net/campuskoder/Default.aspx&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Tampa, FL&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Bill Reiss&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/billreiss"&gt;http://twitter.com/billreiss&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="http://www.bluerosegames.com/silverlightbrasstacks/" target="_blank" href="http://www.bluerosegames.com/silverlightbrasstacks/"&gt;http://www.bluerosegames.com/silverlightbrasstacks/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Tampa, FL&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Rob Zelt&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/robzelt"&gt;http://twitter.com/robzelt&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="www.robzel" target="_blank" href="http://www.robzelt.com/blog/"&gt;http://www.robzelt.com/blog/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;NC&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Keith Elder&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/keithelder"&gt;http://twitter.com/keithelder&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="http://keithelder.net/blog/" target="_blank" href="http://keithelder.net/blog/"&gt;http://keithelder.net/blog/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Hattiesburg, MS&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Todd Miranda&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/tmiranda"&gt;http://twitter.com/tmiranda&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="http://www.xperimentality.com/" target="_blank" href="http://www.xperimentality.com/"&gt;http://www.xperimentality.com/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Alabama&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Alan Stevens&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/alanstevens"&gt;http://twitter.com/alanstevens&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="netcave.org" target="_blank" href="http://netcave.org/"&gt;http://netcave.org/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Knoxville, TN&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Jeff Barnes&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/jeff_barnes"&gt;http://twitter.com/jeff_barnes&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="http://jeffbarnes.net/blog/" target="_blank" href="http://jeffbarnes.net/blog/"&gt;http://jeffbarnes.net/blog/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Birmingham, AL&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Dennis Bottjer&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/dbottjer"&gt;http://twitter.com/dbottjer&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="http://drowningintechnicaldebt.com/blogs/dennisbottjer/default.aspx" target="_blank" href="http://drowningintechnicaldebt.com/blogs/dennisbottjer/default.aspx"&gt;http://drowningintechnicaldebt.com/blogs/dennisbottjer/default.aspx&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Tulsa, OK&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Shervin Shakibi&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a title="http://twitter.com/ShervSherv" target="_blank" href="http://twitter.com/ShervSherv"&gt;http://twitter.com/ShervSherv&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="Shervin's Blog" target="_blank" href="http://www.geekswithblogs.com/shervin/Default.aspx"&gt;http://www.geekswithblogs.com/shervin/Default.aspx&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Boca Raton, FL&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Simone Chiaretta&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/simonech"&gt;http://twitter.com/simonech&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="http://codeclimber.net.nz/" target="_blank" href="http://codeclimber.net.nz/"&gt;http://codeclimber.net.nz/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Milan, Italy&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Stephen Foskett&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/sfoskett"&gt;http://twitter.com/sfoskett&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a target="_blank" href="http://blog.fosketts.net/"&gt;http://blog.fosketts.net/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Wooster, OH&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Dave Noderer&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a title="http://twitter.com/davenoderer" href="http://twitter.com/davenoderer"&gt;http://twitter.com/davenoderer&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="http://geekswithblogs.net/dnoderer/Default.aspx" href="http://geekswithblogs.net/dnoderer/Default.aspx"&gt;http://geekswithblogs.net/dnoderer/Default.aspx&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Deerfield Beach, FL&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;David Silverlight&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a title="http://twitter.com/HeadGeek" href="http://twitter.com/HeadGeek"&gt;http://twitter.com/HeadGeek&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt; &lt;/td&gt;        &lt;td valign="top" width="169"&gt;Miami, FL&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Bill Pytlovany&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/BillP"&gt;http://twitter.com/BillP&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="bitsfrombill.com" target="_blank" href="http://bitsfrombill.com/"&gt;http://bitsfrombill.com/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Scotia, NY&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Joseph Guadagno&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a title="http://twitter.com/jguadagno" target="_blank" href="http://twitter.com/jguadagno"&gt;http://twitter.com/jguadagno&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="http://www.josephguadagno.net/default.aspx?AspxAutoDetectCookieSupport=1" target="_blank" href="http://www.josephguadagno.net/default.aspx?AspxAutoDetectCookieSupport=1"&gt;http://www.josephguadagno.net/default.aspx?AspxAutoDetectCookieSupport=1&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Chandler, AZ&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Tony Ramos&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/tonyramos"&gt;http://twitter.com/tonyramos&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a title="tonyramos.com" target="_blank" href="http://tonyramos.com/"&gt;http://tonyramos.com/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Cleveland, OH&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="147"&gt;Marques Lyons&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a title="tromboneforhire" target="_blank" href="http://www.twitter.com/tromboneforhire"&gt;http://www.twitter.com/tromboneforhire&lt;/a&gt;&lt;a href="http://www.insidethecircle.net/"&gt;&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="539"&gt;&lt;a href="http://www.insidethecircle.net/"&gt;&lt;/a&gt;&lt;a href="http://www.insidethecircle.net/"&gt;&lt;/a&gt;&lt;a title="http://www.insidethecircle.net/" target="_blank" href="http://www.insidethecircle.net/"&gt;http://www.insidethecircle.net/&lt;/a&gt;&lt;a href="http://www.insidethecircle.net/"&gt;&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="169"&gt;Anaheim, CA&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h1&gt;&lt;strong&gt;Microsoft Employees&lt;/strong&gt;&lt;/h1&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="1101"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="154"&gt;&lt;strong&gt;&lt;u&gt;Name&lt;/u&gt;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;strong&gt;&lt;u&gt;Twitter&lt;/u&gt;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="301"&gt;&lt;strong&gt;&lt;u&gt;Blog&lt;/u&gt;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="400"&gt;&lt;strong&gt;&lt;u&gt;Focus Area/Position&lt;/u&gt;&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="154"&gt;Kevin Pilch-Bisson&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/Pilchie"&gt;http://twitter.com/Pilchie&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="301"&gt;&lt;a title="http://blogs.msdn.com/kevinpilchbisson/" target="_blank" href="http://blogs.msdn.com/kevinpilchbisson"&gt;http://blogs.msdn.com/kevinpilchbisson&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="400"&gt;Dev Lead – C#&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="154"&gt;Jake Grey&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/jake_grey"&gt;http://twitter.com/jake_grey&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="301"&gt; &lt;/td&gt;        &lt;td valign="top" width="400"&gt;MVP Lead - C# &amp;amp; Security, Community and Online Support&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="154"&gt;P.J. Forgione&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a title="http://twitter.com/pj_forgione" target="_blank" href="http://twitter.com/pj_forgione"&gt;http://twitter.com/pj_forgione&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="301"&gt; &lt;/td&gt;        &lt;td valign="top" width="400"&gt;MVP Lead – Visual Basic&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="154"&gt;Joe Healy&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a target="_blank" href="http://twitter.com/devfish"&gt;http://twitter.com/devfish&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="301"&gt;&lt;a target="_blank" href="http://devfish.net/"&gt;http://devfish.net&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="400"&gt;Developer Evangelist – Florida&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="154"&gt;Scott Hanselman&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a title="@shanselman" target="_blank" href="http://twitter.com/shanselman/"&gt;http://twitter.com/shanselman/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="301"&gt;&lt;a href="http://www.hanselman.com/blog"&gt;http://www.hanselman.com/blog&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="400"&gt;Principal Program Manager&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="154"&gt;Luis Daniel&lt;/td&gt;        &lt;td valign="top" width="244"&gt;&lt;a title="luisdans" target="_blank" href="http://twitter.com/luisdans"&gt;http://twitter.com/luisdans&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="301"&gt;&lt;a title="http://blogs.msdn.com/luisdans/" target="_blank" href="http://blogs.msdn.com/luisdans/"&gt;http://blogs.msdn.com/luisdans/&lt;/a&gt;&lt;/td&gt;        &lt;td valign="top" width="400"&gt;Developer Platform Evangelism Area Lead – Latin America&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:38f7749d-8637-46ab-833b-002cda248023" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/MVP09" rel="tag"&gt;MVP09&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Twitter" rel="tag"&gt;Twitter&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129763"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129763" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/129763.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/03/01/mvp-summit-2009-twitters.aspx</guid>
            <pubDate>Sun, 01 Mar 2009 23:45:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/129763.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/03/01/mvp-summit-2009-twitters.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/129763.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/129763.aspx</trackback:ping>
        </item>
        <item>
            <title>Visual Studio 2008 Debugging Tricks &amp;ndash; Multi-Threaded Debugging</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-tricks-ndash-multi-threaded-debugging.aspx</link>
            <description>&lt;p&gt;We previously talked about the &lt;a href="http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-tricks-ndash-advanced-breakpoints.aspx" target="_blank"&gt;Location, Condition and Hit Count&lt;/a&gt; modifiers for advanced breakpoints. These are great for a lot of different debugging scenarios, but what about tricks specifically for debugging multi-threaded applications. If you have ever tried to debug a multi-threaded application you know that it isn’t always the easiest experience. &lt;/p&gt;  &lt;p&gt;Hopefully everyone is already familiar with the Treads window. If you aren’t, it is accessed through the Debug|Windows|Threads menu or the Ctrl+D, T keyboard shortcut. The threads window is only available when you are actively in a debug session.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/VisualStudio2008DebuggingTricksMultiThre_D278/image_2.png"&gt;&lt;img style="border-right-width: 0px; margin: 0px 5px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" align="left" src="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/VisualStudio2008DebuggingTricksMultiThre_D278/image_thumb.png" width="209" height="211" /&gt;&lt;/a&gt;The Threads window provides a few useful tricks of it’s own that you might not be aware of. The first three tricks are available from the context menu.&lt;/p&gt;  &lt;p&gt;You can freeze threads to avoid bouncing around when single stepping and you can switch to a specific thread.&lt;/p&gt;  &lt;p&gt;Giving threads meaningful names is very helpful in helping to identify your threads and not all threads have a meaningful name (or in some cases, they don’t have a name at all). Fortunately, you can rename threads directly from the Threads window as well. You can do this using either the Rename context menu or simply clicking in the Name column and typing.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="" border="0" alt="" src="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/VisualStudio2008DebuggingTricksMultiThre_D278/080107_2004_NeatNewMult2_3.png" width="386" height="97" /&gt;  &lt;/p&gt;  &lt;p&gt;The Threads window also makes it easier to identify the main and worker threads by using the green and yellow color squares. Perhaps the most interesting new feature is the ability to flag threads. Once a thread is flagged, you can use use the Debug Location toolbar to only display those flagged threads. This is a great simplifying option for applications that have a lot of threads.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="" border="0" alt="" src="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/VisualStudio2008DebuggingTricksMultiThre_D278/image_5.png" width="270" height="124" /&gt; &lt;/p&gt;  &lt;p&gt;One other benefit of the Threads window is that by hovering the mouse over the Location column (or the current thread indicator, which is the yellow arrow) you can see the call stack for that thread. This is really useful if you just need to check the call stack quickly and don’t actually want to switch to that thread.&lt;/p&gt;  &lt;p&gt;One last trick to mention is the Show Threads in Source option. &lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/VisualStudio2008DebuggingTricksMultiThre_D278/image%5B142%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image_thumb[58]" border="0" alt="image_thumb[58]" align="right" src="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/VisualStudio2008DebuggingTricksMultiThre_D278/image_thumb%5B58%5D_24496d81-8735-4631-b8b0-198b3a3fa12d.png" width="244" height="47" /&gt;&lt;/a&gt;This is available from the Threads window context menu and also the Debug toolbar. &lt;/p&gt;  &lt;p&gt;When this option is turned on you get some very nice visual indicators to show information about your threads. If you look carefully, you will see that the breakpoint symbol actually has a red and blue squiggly line through it and, just below that you see a similar marker. If you hover the mouse over the threads marker you will see a debug tool tip that shows you all of the threads that are executing at that location. If you notice there are also some source code lines shaded in grey, which indicates that another thread is executing at the same location. &lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="" border="0" alt="" src="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/VisualStudio2008DebuggingTricksMultiThre_D278/080107_2004_NeatNewMult1_3.png" width="564" height="172" /&gt; &lt;/p&gt;  &lt;p&gt;One word of caution with the Show Threads In Source option: You must have this option enabled before your debug session begins in order for it to behave correctly.&lt;/p&gt;  &lt;p&gt;Now, let’s take a look now at the Filter modifier. This is used to restrict the breakpoint to only being set in certain processes and/or threads. When you choose the filter modifier, the Breakpoint Filter dialog is displayed&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="" border="0" alt="" src="http://geekswithblogs.net/images/geekswithblogs_net/sdorman/WindowsLiveWriter/VisualStudio2008DebuggingTricksMultiThre_D278/image%5B140%5D_3.png" width="505" height="410" /&gt;&lt;/p&gt;  &lt;p&gt;which allows you to specify the machine name, process id (or name), and/or the thread id (or name). Using the threads shown earlier, if we wanted to restrict a breakpoint to only being set in Thread 3 we could set the filter to either of these expressions:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ThreadId = 2020 &lt;/li&gt;    &lt;li&gt;ThreadName = "Thread 3" &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Two important things to be aware of here are that the machine/process/thread keywords are case insensitive and that the “==” operator is supported.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e5ff7221-12bf-4fbe-b949-54497c96a9d7" class="wlWriterSmartContent"&gt;&lt;i&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Visual+Studio" rel="tag"&gt;Visual Studio&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Advanced" rel="tag"&gt;Advanced&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Debugging" rel="tag"&gt;Debugging&lt;/a&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-tricks-ndash-multi-threaded-debugging.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-tricks-ndash-multi-threaded-debugging.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129401"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129401" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/129401.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-tricks-ndash-multi-threaded-debugging.aspx</guid>
            <pubDate>Sat, 14 Feb 2009 22:16:53 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/129401.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-tricks-ndash-multi-threaded-debugging.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/129401.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/129401.aspx</trackback:ping>
        </item>
        <item>
            <title>Adding custom assertions to MSTest</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx</link>
            <description>&lt;p&gt;In my last &lt;a href="http://geekswithblogs.net/sdorman/archive/2009/01/31/migrating-from-nunit-to-mstest.aspx"&gt;post&lt;/a&gt; I talked about migrating from NUnit to MSTest and mentioned that there were some Assert methods that NUnit provided which did not have corresponding methods in MSTest. For reference, those unavailable Asserts are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Assert.IsNaN &lt;/li&gt;    &lt;li&gt;Assert.IsEmpty &lt;/li&gt;    &lt;li&gt;Assert.IsNotEmpty &lt;/li&gt;    &lt;li&gt;Assert.Greater &lt;/li&gt;    &lt;li&gt;Assert.GreaterOrEqual &lt;/li&gt;    &lt;li&gt;Assert.Less &lt;/li&gt;    &lt;li&gt;Assert.LessOrEqual &lt;/li&gt;    &lt;li&gt;Assert.IsAssignableFrom &lt;/li&gt;    &lt;li&gt;Assert.IsNotAssignableFrom &lt;/li&gt;    &lt;li&gt;CollectionAssert.IsEmpty &lt;/li&gt;    &lt;li&gt;CollectionAssert.IsNotEmpty &lt;/li&gt;    &lt;li&gt;StringAssert.AreEqualIgnoringCase &lt;/li&gt;    &lt;li&gt;StringAssert.IsMatch &lt;/li&gt;    &lt;li&gt;FileAssert.AreEqual &lt;/li&gt;    &lt;li&gt;FileAssert.AreNotEqual&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Even though MSTest does not provide the same extensibility capabilities as NUnit, it is still possible to add these missing Assert methods by creating your own class library to reference in your unit test project.&lt;/p&gt;  &lt;p&gt;In order to help ease any potential issues in migrating from NUnit to MSTest caused by the lack of these Assert methods, I have created a class library that includes all of them except the FileAssert methods and StringAssert.IsMatch. These methods should behave exactly like they do in NUnit. (In fact, the unit tests are the same ones used in NUnit, with only a few minor differences.)&lt;/p&gt;  &lt;p&gt;The following table shows the changes you will need to make in order to use the new asserts. As you can see, the changes can be easily made using search and replace.&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="460"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="224"&gt;&lt;strong&gt;NUnit&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="46"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="188"&gt;&lt;strong&gt;MSTest Extension Library&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;Assert.IsNaN&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;ConditionAssert.IsNaN&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;Assert.IsEmpty&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;CustomAssert.IsEmpty&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;Assert.IsNotEmpty&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;CustomAssert.IsNotEmpty&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;Assert.Greater&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;ConditionAssert.Greater&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;Assert.GreaterOrEqual&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;ConditionAssert.GreaterOrEqual&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;Assert.Less&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;ConditionAssert.Less&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;Assert.LessOrEqual&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;ConditionAssert.LessOrEqual&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;Assert.IsAssignableFrom&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;TypeAssert.IsAssignableFrom&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;Assert.IsNotAssignableFrom&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;TypeAssert.IsNotAssignableFrom&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;CollectionAssert.IsEmpty&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;CustomAssert.IsEmpty&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;CollectionAssert.IsNotEmpty&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;CustomAssert.IsNotEmpty&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="224"&gt;StringAssert.AreEqualIgnoringCase&lt;/td&gt;        &lt;td valign="top" width="46"&gt; &lt;/td&gt;        &lt;td valign="top" width="188"&gt;CustomAssert.IsNotEmpty&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;I also added the &lt;a href="http://geekswithblogs.net/sdorman/archive/2009/01/17/unit-testing-and-expected-exceptions.aspx"&gt;ExceptionAssert&lt;/a&gt; class from my post talking about the dangers of using the ExpectedException attribute.&lt;/p&gt;  &lt;p&gt;You can &lt;a href="http://cid-93d618d639ec9651.skydrive.live.com/self.aspx/Public/Campari.Software.UnitTest.Framework.zip"&gt;download&lt;/a&gt; the class from my &lt;a href="http://skydrive.live.com/" target="_blank"&gt;SkyDrive&lt;/a&gt; public folder.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9a58342b-8105-4724-bead-1f703b65db4d" class="wlWriterSmartContent"&gt;&lt;i&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Unit+Tests" rel="tag"&gt;Unit Tests&lt;/a&gt;, &lt;a href="http://technorati.com/tags/MSTest" rel="tag"&gt;MSTest&lt;/a&gt;, &lt;a href="http://technorati.com/tags/NUnit" rel="tag"&gt;NUnit&lt;/a&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129112"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129112" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/129112.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx</guid>
            <pubDate>Sun, 01 Feb 2009 03:10:45 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/129112.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/01/31/adding-custom-assertions-to-mstest.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/129112.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/129112.aspx</trackback:ping>
        </item>
        <item>
            <title>Migrating from NUnit to MSTest</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/01/31/migrating-from-nunit-to-mstest.aspx</link>
            <description>&lt;p&gt;I have been writing a lot of unit tests over the past month using the Microsoft Test (MSTest) unit testing framework that is now part of Visual Studio 2008 Professional Edition (and higher SKUs). Currently I have about 223 unit tests covering 39 classes (about 18K lines of code, 325 methods and 176 properties) with a code coverage percent of 97%.&lt;/p&gt;  &lt;p&gt;The classes that I’m testing previously had NUnit tests (although not as many as I currently have) and used NCover and NCover Explorer for my code coverage analysis. I had looked at MSTest when it first came out and decided against using it because it was only available in the Visual Studio Team System SKUs, which meant it wasn’t generally available to the development community at large, and required Visual Studio to be installed in order to use it.&lt;/p&gt;  &lt;p&gt;Since then, Microsoft has addressed both of those issues and I decided it was time to give MSTest another look. In doing so, I decided to see if it would be possible to leverage the existing NUnit tests rather than starting completely from scratch. One word of caution…if you use the newer constraint model in NUnit, your unit tests will not easily migrate to MSTest and you are probably better off starting fresh using MSTest.&lt;/p&gt;  &lt;p&gt;It turned out to be a relatively painless process that can be broken down to the following steps:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Replace the reference to the Nunit.Framework assembly with a reference to the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly.&lt;/li&gt;    &lt;li&gt;Replace the “using Nunit.Framework” line with “using Microsoft.VisualStudio.TestTools.UnitTesting”&lt;/li&gt;    &lt;li&gt;For each file containing unit tests, replace the following attributes (you can use global search and replace for this):     &lt;br /&gt;&lt;/li&gt;    &lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="200"&gt;&lt;strong&gt;NUnit Attribute&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="200"&gt;&lt;strong&gt;MSTest Attribute&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;TestFixture&lt;/td&gt;          &lt;td valign="top" width="200"&gt;TestClass&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Test&lt;/td&gt;          &lt;td valign="top" width="200"&gt;TestMethod&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;SetUp&lt;/td&gt;          &lt;td valign="top" width="200"&gt;TestInitialize&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;TearDown&lt;/td&gt;          &lt;td valign="top" width="200"&gt;TestCleanup&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;TestFixtureSetup&lt;/td&gt;          &lt;td valign="top" width="200"&gt;ClassInitialize&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;TestFixtureTearDown&lt;/td&gt;          &lt;td valign="top" width="200"&gt;ClassCleanup&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt; &lt;/td&gt;          &lt;td valign="top" width="200"&gt; &lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt;    &lt;li&gt;Replace the following method calls:     &lt;br /&gt;&lt;/li&gt;    &lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" width="200"&gt;&lt;strong&gt;NUnit Method Code&lt;/strong&gt;&lt;/td&gt;          &lt;td valign="top" width="200"&gt;&lt;strong&gt;MSTest Method Call&lt;/strong&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td valign="top" width="200"&gt;Assert.Ignore&lt;/td&gt;          &lt;td valign="top" width="200"&gt;Assert.Inconclusive&lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/ol&gt;  &lt;p&gt;At this point, the easy stuff is done, but Visual Studio will not recognize your project as an MSTest unit test project. (This only applies if you don’t create a new Unit Test project in Visual Studio to hold your converted tests.) If that’s the case, you need to edit the project file using a text editor and add the following tag to the first &amp;lt;PropertyGroup&amp;gt; in the XML:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ProjectTypeGuids&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ProjectTypeGuids&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;(These Guids are for a C# project. For a VB project, replace the second Guid with {F184B08F-C81C-45F6-A57F-5ABD9991F28F}.)&lt;/p&gt;

&lt;p&gt;Depending on the complexity of your original NUnit tests, you may be done at this point. However, if you relied on NUnit TestFixtureSetup or TestFixtureTearDown methods you will need to make a few other changes.&lt;/p&gt;

&lt;p&gt;In NUnit, these attributes would be applied to a non-static void method that takes no parameters. For MSTest, the methods must be static and the ClassInitialize method must take a &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.testcontext.aspx" target="_blank"&gt;TestContext&lt;/a&gt; parameter. This also means that you should add a public TestContext property as well. What you end up with is code that looks like this:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; height: 239px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; TestContext testContextInstance;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;///Gets or sets the test context which provides&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; &lt;span style="color: #008000"&gt;///information about and functionality for the current test run.&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt; &lt;span style="color: #008000"&gt;///&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; TestContext TestContext&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     get&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; testContextInstance;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;     set&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;         testContextInstance = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt; [ClassInitialize()]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum20"&gt;  20:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ClassInit(TestContext context)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum21"&gt;  21:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum22"&gt;  22:&lt;/span&gt;     MessageBox.Show(&lt;span style="color: #006080"&gt;"ClassInit "&lt;/span&gt; + context.TestName);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum23"&gt;  23:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum24"&gt;  24:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum25"&gt;  25:&lt;/span&gt; [ClassCleanup()]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum26"&gt;  26:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ClassCleanup()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum27"&gt;  27:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum28"&gt;  28:&lt;/span&gt;     MessageBox.Show(&lt;span style="color: #006080"&gt;"ClassCleanup"&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum29"&gt;  29:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;If you had code in your NUnit TestFixtureSetup or TestFixtureTearDown methods that manipulated non-static data you will need to rework your tests or move that code to a constructor/finalizer combination to achieve the same results.&lt;/p&gt;

&lt;p&gt;Unfortunately, the Assert (and related classes) used by MSTest are not as complete as the ones offered by NUnit so you may also end up changing some of your tests. The following Asserts are not available in MSTest:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Assert.IsNaN&lt;/li&gt;

  &lt;li&gt;Assert.IsEmpty&lt;/li&gt;

  &lt;li&gt;Assert.IsNotEmpty&lt;/li&gt;

  &lt;li&gt;Assert.Greater&lt;/li&gt;

  &lt;li&gt;Assert.GreaterOrEqual&lt;/li&gt;

  &lt;li&gt;Assert.Less&lt;/li&gt;

  &lt;li&gt;Assert.LessOrEqual&lt;/li&gt;

  &lt;li&gt;Assert.IsAssignableFrom&lt;/li&gt;

  &lt;li&gt;Assert.IsNotAssignableFrom&lt;/li&gt;

  &lt;li&gt;CollectionAssert.IsEmpty&lt;/li&gt;

  &lt;li&gt;CollectionAssert.IsNotEmpty&lt;/li&gt;

  &lt;li&gt;StringAssert.AreEqualIgnoringCase&lt;/li&gt;

  &lt;li&gt;StringAssert.IsMatch&lt;/li&gt;

  &lt;li&gt;FileAssert.AreEqual&lt;/li&gt;

  &lt;li&gt;FileAssert.AreNotEqual&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You may be tempted to replace StringAssert.IsMatch with StringAssert.Matches from MSTest but they really aren’t equivalent methods. StringAssert.Matches matches the actual result against a regular expression pattern.&lt;/p&gt;

&lt;p&gt;The other thing to watch out for is any place you use the ExpectedException attribute. The syntax is identical between NUnit and MSTest, however the meaning is not. In NUnit, the message parameter is used to verify the message of the exception (by comparing the Message property of the exception with this text). MSTest uses this text as the message to display if the exception is not thrown. There are &lt;a href="http://geekswithblogs.net/sdorman/archive/2009/01/17/unit-testing-and-expected-exceptions.aspx" target="_blank"&gt;better ways&lt;/a&gt; to test for exceptions that don’t use the ExpectedException attribute at all.&lt;/p&gt;

&lt;p&gt;All in all, I’ve been very happy with the relative ease of migrating my NUnit tests to MSTest and using the MSTest framework in general. Yes, there are things that the older unit testing frameworks provide that MSTest does not, but I believe that is just a matter of age. In time, MSTest will offer the same functionality. The nice thing about MSTest is that it is built in to Visual Studio and the code coverage and unit testing capabilities are integrated with the IDE without requiring additional tools, add-ins, macros, or other hoops in order to run your tests and evaluate the results. Everything you need to write effective unit tests, analyze the results and ensure good code coverage is already available to you.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8e530e48-a136-4398-b4d6-7da6416e2cd3" class="wlWriterSmartContent"&gt;&lt;i&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/MSTest" rel="tag"&gt;MSTest&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Unit+Tests" rel="tag"&gt;Unit Tests&lt;/a&gt;, &lt;a href="http://technorati.com/tags/NUnit" rel="tag"&gt;NUnit&lt;/a&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://geekswithblogs.net/sdorman/archive/2009/01/31/migrating-from-nunit-to-mstest.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://geekswithblogs.net/sdorman/archive/2009/01/31/migrating-from-nunit-to-mstest.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129108"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=129108" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/129108.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/01/31/migrating-from-nunit-to-mstest.aspx</guid>
            <pubDate>Sat, 31 Jan 2009 18:39:15 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/129108.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/01/31/migrating-from-nunit-to-mstest.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/129108.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/129108.aspx</trackback:ping>
        </item>
        <item>
            <title>Reading all bytes from a Stream</title>
            <link>http://geekswithblogs.net/sdorman/archive/2009/01/10/reading-all-bytes-from-a-stream.aspx</link>
            <description>&lt;p&gt;I was working on some &lt;a title="Stream Class" target="_blank" href="http://msdn2.microsoft.com/8f86tw9e.aspx"&gt;Stream&lt;/a&gt; extensions today and added a method which will read all of the data from a stream in to a byte array. Some of the classes which derive from Stream provide a similar method (&lt;a title="MemoryStream Class" target="_blank" href="http://msdn2.microsoft.com/9a84386f.aspx"&gt;MemoryStream&lt;/a&gt;, for example, provides a GetBuffer() method to do this). The drawback here is that the base Stream class doesn’t provide an abstract GetBuffer() method; instead it’s up to each individual derived class to implement such a method. This is perfectly reasonable as a stream may not have an underlying buffer for storage like MemoryStream does.&lt;/p&gt;  &lt;p&gt;Taking that into consideration, you read streams the same way. The call to Stream.Read() takes an array of bytes as a buffer to fill during the read operation. It would be nice to be able to get the entire contents of the stream as a byte array.&lt;/p&gt;  &lt;p&gt;Looking around I found an excellent &lt;a target="_blank" href="http://www.yoda.arachsys.com/csharp/readbinary.html"&gt;blog post&lt;/a&gt; by Jon Skeet that provides a few implementations on how to do this. The thing I didn’t like about Jon’s implementation is that it uses Array.Copy in order to resize the array (when there is more data available than the buffer can hold) and to return the properly sized array at the end. (It also has two exit points, which I don’t ordinarily like, but that’s a minor issue.)&lt;/p&gt;  &lt;p&gt;The concern with using Array.Copy is that it accesses the elements by index and must be somewhat generic in order to support all of the .NET languages. Since streams tend to be rather large, this introduces unnecessary overhead. &lt;/p&gt;  &lt;p&gt;At this point, you might be asking “Why unnecessary overhead? How else do you copy arrays?” This question brings us to &lt;a title="Buffer Class" href="http://msdn2.microsoft.com/teyhh36d.aspx"&gt;System.Buffer&lt;/a&gt;, which has been available in .NET since .NET 1.1 although you’ve probably never heard of it until now. A Buffer is designed to manipulate arrays of primitive types and treats each type as a series of bytes without any regard to behavior or limitations. How does this help us? Remember, Stream.Read returns an array of bytes and byte is a primitive type. So we can use Buffer to copy the array elements directly in memory rather than by index, which provides better performance than Array.Copy.&lt;/p&gt;  &lt;p&gt;After modifying Jon’s implementation to use Buffer, fixing an issue I noticed (the stream’s position was never reset to 0 prior to reading) and getting rid of that pesky second exit point, my implementation is:&lt;/p&gt;  &lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 1200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;   &lt;div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;     &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// Reads the contents of the stream into a byte array.&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// data is returned as a byte array. An IOException is&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// thrown if any of the underlying IO calls fail.&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// &amp;lt;param name="stream"&amp;gt;The stream to read.&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// &amp;lt;returns&amp;gt;A byte array containing the contents of the stream.&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// &amp;lt;exception cref="NotSupportedException"&amp;gt;The stream does not support reading.&amp;lt;/exception&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// &amp;lt;exception cref="ObjectDisposedException"&amp;gt;Methods were called after the stream was closed.&amp;lt;/exception&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt; &lt;span style="color: #008000"&gt;/// &amp;lt;exception cref="System.IO.IOException"&amp;gt;An I/O error occurs.&amp;lt;/exception&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] ReadAllBytes(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; Stream source)&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  13:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;long&lt;/span&gt; originalPosition = source.Position;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  14:&lt;/span&gt;     source.Position = 0;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  15:&lt;/span&gt;  &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  16:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  17:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  18:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] readBuffer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[4096];&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  19:&lt;/span&gt;  &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  20:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; totalBytesRead = 0;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  21:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; bytesRead;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  22:&lt;/span&gt;  &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  23:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;while&lt;/span&gt; ((bytesRead = source.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) &amp;gt; 0)&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  24:&lt;/span&gt;         {&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  25:&lt;/span&gt;             totalBytesRead += bytesRead;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  26:&lt;/span&gt;  &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  27:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (totalBytesRead == readBuffer.Length)&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  28:&lt;/span&gt;             {&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  29:&lt;/span&gt;                 &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; nextByte = source.ReadByte();&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  30:&lt;/span&gt;                 &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (nextByte != -1)&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  31:&lt;/span&gt;                 {&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  32:&lt;/span&gt;                     &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] temp = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[readBuffer.Length * 2];&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  33:&lt;/span&gt;                     Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  34:&lt;/span&gt;                     Buffer.SetByte(temp, totalBytesRead, (&lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;)nextByte);&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  35:&lt;/span&gt;                     readBuffer = temp;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  36:&lt;/span&gt;                     totalBytesRead++;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  37:&lt;/span&gt;                 }&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  38:&lt;/span&gt;             }&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  39:&lt;/span&gt;         }&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  40:&lt;/span&gt;  &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  41:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] buffer = readBuffer;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  42:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (readBuffer.Length != totalBytesRead)&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  43:&lt;/span&gt;         {&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  44:&lt;/span&gt;             buffer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[totalBytesRead];&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  45:&lt;/span&gt;             Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  46:&lt;/span&gt;         }&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  47:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; buffer;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  48:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  49:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  50:&lt;/span&gt;     {&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  51:&lt;/span&gt;         source.Position = originalPosition;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  52:&lt;/span&gt;     }&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  53:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;It will still generally use at least 2 arrays (if the original array is already the same size as the total number of bytes read, it will not resize it) and sometimes a 3rd array (created inside the while loop) when the stream is larger than the original array. However, by using the Buffer.BlockCopy and Buffer.SetByte rather than the Array methods it should offer better performance.&lt;/p&gt;

&lt;p&gt;
  &lt;/p&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9c0ac743-4ee7-42c3-91bc-8aa43718286f" class="wlWriterSmartContent"&gt;&lt;i&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;, &lt;a href="http://technorati.com/tags/.NET" rel="tag"&gt;.NET&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Streams" rel="tag"&gt;Streams&lt;/a&gt;, &lt;a href="http://technorati.com/tags/Buffer" rel="tag"&gt;Buffer&lt;/a&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://geekswithblogs.net/sdorman/archive/2009/01/10/reading-all-bytes-from-a-stream.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://geekswithblogs.net/sdorman/archive/2009/01/10/reading-all-bytes-from-a-stream.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=128549"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=128549" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/sdorman/aggbug/128549.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Dorman</dc:creator>
            <guid>http://geekswithblogs.net/sdorman/archive/2009/01/10/reading-all-bytes-from-a-stream.aspx</guid>
            <pubDate>Sat, 10 Jan 2009 21:24:47 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sdorman/comments/128549.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sdorman/archive/2009/01/10/reading-all-bytes-from-a-stream.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/sdorman/comments/commentRss/128549.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/sdorman/services/trackbacks/128549.aspx</trackback:ping>
        </item>
    </channel>
</rss>