Re: fxcop

Rhys Weatherley <[email protected]>
Newsgroups gmane.comp.gnu.dotgnu.developer
Organization Southern Storm Software, Pty Ltd
Message-ID <[email protected]>
On Saturday 01 May 2004 06:51 am, Marcus wrote:
> The "if (val.Length == 0)" does not seem very intuitive in the context of
> determine whether a string is empty or not. A better solution would seem to
> be to optimize the comparison of "if (val == String.Empty)".
>
> Also, my understanding was that all string constants declared in a program
> would be interned, so that a simple pointer comparison could be done.

Interning would apply to the "" or String.Empty part of the comparison, but 
not necessarily to the contents of "val".  A simple pointer comparison would 
fail on the following code:

       String x = " ";
       String y = x.Trim();
       if(Object.ReferenceEquals(y, "")) ...

i.e. calculated string results are not necessarily interned, and so their 
pointers may not be the same as interned strings.  It also varies from system 
to system a little bit (I had to use an empty StringBuilder to get the same 
effect under csc).

> Given all that, I'm not sure why the val.Length version is so much faster.

Portable.NET inlines "val.Length", converting it into a simple "fetch int from 
field" opcode.  Portable.NET also inlines string comparison, but it needs to 
do more work than a simple field fetch.

Cheers,

Rhys.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.