<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://davesbox.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Dave's Box : BCL, FxCop</title><link>http://davesbox.com/archive/tags/BCL/FxCop/default.aspx</link><description>Tags: BCL, FxCop</description><dc:language>en</dc:language><generator>CommunityServer 2008 (Build: 30417.1769)</generator><item><title>Breaking changes to the String class</title><link>http://davesbox.com/archive/2008/11/12/breaking-changes-to-the-string-class.aspx</link><pubDate>Wed, 12 Nov 2008 15:00:43 GMT</pubDate><guid isPermaLink="false">2122c344-89bc-4cd7-b145-b0515ba3439f:971</guid><dc:creator>David Kean</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://davesbox.com/rsscomments.aspx?PostID=971</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://davesbox.com/commentapi.aspx?PostID=971</wfw:comment><comments>http://davesbox.com/archive/2008/11/12/breaking-changes-to-the-string-class.aspx#comments</comments><description>&lt;p&gt;You may have already read Justin Van Patten&amp;#39;s post about the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx"&gt;upcoming breaking changes to the String class for .NET 4.0&lt;/a&gt;. This change will affect the behavior of the &lt;strong&gt;String.StartsWith&lt;/strong&gt;, &lt;strong&gt;String.&lt;/strong&gt;&lt;strong&gt;EndsWith&lt;/strong&gt;, &lt;strong&gt;String.&lt;/strong&gt;&lt;strong&gt;IndexOf&lt;/strong&gt; and &lt;strong&gt;String.&lt;/strong&gt;&lt;strong&gt;LastIndexOf&lt;/strong&gt; methods by changing them to perform an ordinal (byte-for-byte) comparison by default instead of a culture-sensitive comparison using &lt;strong&gt;CultureInfo.CurrentCulture. &lt;/strong&gt;In addition, the default overloads of &lt;strong&gt;String.ToUpper,&lt;/strong&gt; &lt;strong&gt;String.ToLower&lt;/strong&gt;,&lt;strong&gt; Char.ToUpper &lt;/strong&gt;and &lt;strong&gt;Char.ToLower&lt;/strong&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;will be changed to use &lt;strong&gt;CultureInfo.InvariantCulture&lt;/strong&gt; instead of &lt;strong&gt;CultureInfo.CurrentCulture&lt;/strong&gt;.&lt;/p&gt; &lt;h4&gt;&lt;strong&gt;How does this change affect me? &lt;/strong&gt;&lt;/h4&gt; &lt;p&gt;If you are using an overload of one of the above methods that does not take a &lt;strong&gt;StringComparison&lt;/strong&gt; or &lt;strong&gt;CultureInfo&lt;/strong&gt; as a parameter, then your application or library will be affected when you move it to .NET 4.0. &lt;strong&gt;String.Compare&lt;/strong&gt; and &lt;strong&gt;String.CompareTo&lt;/strong&gt; are not being changed.&lt;/p&gt; &lt;p&gt;An example of the kind of behavior change you could expect, is the following:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;static void &lt;/span&gt;Main()
{
    &lt;span style="color:blue;"&gt;int &lt;/span&gt;index = &lt;span style="color:#a31515;"&gt;&amp;quot;encyclopædia&amp;quot;&lt;/span&gt;.IndexOf(&lt;span style="color:#a31515;"&gt;&amp;quot;encyclopaedia&amp;quot;&lt;/span&gt;);

    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(index);
}&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;On versions previous to .NET 4.0, this will output &lt;em&gt;0&lt;/em&gt; when the user&amp;#39;s current locale is set to some cultures, such &lt;em&gt;en-AU, English (Australia)&lt;/em&gt;, and &lt;em&gt;-1&lt;/em&gt; when the user&amp;#39;s current locale is other cultures, such &lt;em&gt;nn-NO, Norwegian, Nynorsk (Norway)&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;However in .NET 4.0, the above comparison will always output &lt;em&gt;-1&lt;/em&gt;, regardless of the user&amp;#39;s current locale.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Why was this change made?&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;The comparison methods on the String class have always been a little schizophrenic; some methods, &lt;strong&gt;==&lt;/strong&gt;, &lt;strong&gt;String.Equals&lt;/strong&gt; and &lt;strong&gt;String.Contains &lt;/strong&gt;for example, perform an ordinal comparison by default, whereas the above methods, as well &lt;strong&gt;String.Compare&lt;/strong&gt; and &lt;strong&gt;String&lt;/strong&gt;.&lt;strong&gt;CompareTo,&lt;/strong&gt; perform a culture-sensitive comparison. Because they use the current culture, the default culture-sensitive comparisons can also vary between systems, users and even during the same application session. Which, as pointed out in Justin&amp;#39;s post, can lead to security vulnerabilities in applications that make security decisions using the default overloads. The planned breaking changes bring the majority of these methods inline with each other to have the same default behavior.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;How do I find and fix these comparisons?&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;To start, you should run &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9aeaa970-f281-4fb0-aba1-d59d7ed09772&amp;amp;DisplayLang=en"&gt;FxCop&lt;/a&gt; or &lt;a href="http://msdn.microsoft.com/en-us/library/3z0aeatx.aspx"&gt;Visual Studio Code Analysis&lt;/a&gt; over your code base. The rules &lt;a href="http://msdn.microsoft.com/en-us/library/bb386080.aspx"&gt;Specify StringComparison&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms182189.aspx"&gt;Specify CultureInfo&lt;/a&gt; will fire on call sites that do not explicitly specify a &lt;strong&gt;StringComparison&lt;/strong&gt; or &lt;strong&gt;CultureInfo&lt;/strong&gt;. &lt;a href="http://msdn.microsoft.com/en-us/library/bb385972.aspx"&gt;Specify ordinal StringComparison&lt;/a&gt; will fire when you use invariant culture instead of ordinal to compare, which is almost always wrong.&lt;/p&gt;
&lt;p&gt;To help determine the correct &lt;strong&gt;StringComparison&lt;/strong&gt; or &lt;strong&gt;CultureInfo&lt;/strong&gt; to use, you cannot go past the excellent article &lt;a href="http://msdn.microsoft.com/en-us/library/ms973919.aspx"&gt;New Recommendations for Using Strings in .NET 2.0&lt;/a&gt;. In particular, the section under &lt;strong&gt;Choosing a StringComparison Member for Your Method Call &lt;/strong&gt;provides a table detailing common operations and comparisons to use.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://davesbox.com/aggbug.aspx?PostID=971" width="1" height="1"&gt;</description><category domain="http://davesbox.com/archive/tags/FxCop/default.aspx">FxCop</category><category domain="http://davesbox.com/archive/tags/BCL/default.aspx">BCL</category><category domain="http://davesbox.com/archive/tags/.NET+4.0/default.aspx">.NET 4.0</category><category domain="http://davesbox.com/archive/tags/Compatibility/default.aspx">Compatibility</category></item></channel></rss>