[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [DotGNU]fxcop
From: |
Rhys Weatherley |
Subject: |
Re: [DotGNU]fxcop |
Date: |
Sat, 1 May 2004 07:10:13 +1000 |
User-agent: |
KMail/1.4.3 |
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.