Shall I now always use String.Empty instead of ""

D

Dave Sexton

Hi,

I'm one of those people too - no secret here.

It works most of the time for me, but it can be painfully slow or not load
at all sometimes. I assume they are making some major modifications to the
site.

Although, msdn.microsoft.com/library and the search itself seem to work just
fine, but once you click a search result that redirects you to msdn2, it's
20/80 ;)

--
Dave Sexton

clintonG said:
Sorry to go OT -- for a monet -- but you're able to load documentation at
msdn2.microsoft.com?
Quite a few people can not load pages at msdn2 for many weeks now and
trying to figure out why.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP http://wikimapia.org/#y=43038073&x=-88043838&z=17&l=0&m=h

Dave Sexton said:
Hi Jon,

Your example code has been bothering me - it should work, however it
doesn't and I can't figure out why :p

But if you think that's weird, check out the following documentation I
found on MSDN:

"String.Intern Method"
http://msdn2.microsoft.com/en-us/library/system.string.intern.aspx

<quote>
Version Considerations
Starting with the .NET Framework version 2.0, there is a behavioral
change in the Intern method. In the following C# code sequence, the
variable str1 is assigned a reference to Empty, the variable str2 is
assigned the reference to Empty that is returned by the Intern method,
then the references contained in str1 and str2 are compared for equality.

string str1 = String.Empty;
string str2 = String.Intern(String.Empty);
if ((object) str1) == ((object) str2) .

In the .NET Framework version 1.1, str1 and str2 are not equal, but
starting in the .NET Framework version 2.0, str1 and str2 are equal.
</quote>

Not in my test :|

string str1 = String.Empty;
string str2 = String.Intern(String.Empty);

// false
Console.WriteLine((object) str1 == (object) str2);

// false
Console.WriteLine((object) string.Empty == (object) "");

// true
Console.WriteLine((object) "" == (object) "");


Using Reflector I verified that string.Empty is assigned the empty
literal, "", in the class constructor (.cctor). I also verified, through
testing, that interning works across assemblies; so I'm stumped here.

The earliest version of the framework that I have installed on my machine
is 2.0, verified in Add or Remove Programs. I also have 3.0 installed,
if that makes a difference.

Anyone know what's going on here?

--
Dave Sexton

Jon Shemitz said:
Morten Wennevik wrote:

"" is easy to write, easy to read, and near impossible to
misunderstand.
It will however create an object where String.Empty would not, but the
""
object is reused throughout the lifespawn of your application so the
difference can therefore be ignored.

I was surprised to read that "" will create an object where
String.Empty will not - surely String.Empty is just a static field
that points to an interned "" constant?

Sure enough, Reflector shows that String.Empty is implemented as

class string
{
static readonly string Empty = "";
}

... yet the below code shows that while both "" and String.Empty are
interned, "" is not reference-equal to String.Empty. What gives? I
thought the intern table was maintained across assembly boundaries, so
it shouldn't matter that String.Empty comes from mscorlib.dll while ""
comes from the app assembly.

//

using System;

namespace StringEmptyTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Object.ReferenceEquals(String.Empty, ""));
Console.WriteLine(String.IsInterned("") != null);
Console.WriteLine(String.IsInterned(String.Empty) != null);
Console.ReadLine();
}
}
}


--

.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
 
G

Gabriel Lozano-Morán

Like I said, whatever gets you off. It is like starting with a the letter
"p" for all your method arguments. Some do it and some don't.

Gabriel
 
G

Gabriel Lozano-Morán

Correct at the moment it is only for NGEN-ed assemblies but if I am not
mistaken it is possible that in future versions it will be possible to
disable the intern pool imho that's the reason why the documentation might
seem misleading. Personally I have never understand the need for string
interning and the overhead that comes with it because how many times will
you have two or more exact string literals?

Gabriel
 
J

Jon Shemitz

Gabriel Lozano-Morán said:
Correct at the moment it is only for NGEN-ed assemblies but if I am not
mistaken it is possible that in future versions it will be possible to
disable the intern pool imho that's the reason why the documentation might
seem misleading. Personally I have never understand the need for string
interning and the overhead that comes with it because how many times will
you have two or more exact string literals?

Depends on how much HTML/XML you're generating, I guess.
 
C

clintonG

I urge you to respond to my recent news article posted in several Microsoft
newsgroups related to MSDN and ASP.NET development. The subject is

"For Microsoft Partners and Customers Who Can't Download or Access MSDN2..."

Take your own stance of course but please let your voice be heard about
these server failures.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP http://wikimapia.org/#y=43038073&x=-88043838&z=17&l=0&m=h




Dave Sexton said:
Hi Clinton,

:)

Even stranger, FireFox appears to be slower than IE7 when loading msdn2
now (when it works :)

--
Dave Sexton

clintonG said:
Misery loves company ;-)

<%= Clinton

Dave Sexton said:
Hi,

I'm one of those people too - no secret here.

It works most of the time for me, but it can be painfully slow or not
load at all sometimes. I assume they are making some major
modifications to the site.

Although, msdn.microsoft.com/library and the search itself seem to work
just fine, but once you click a search result that redirects you to
msdn2, it's 20/80 ;)

--
Dave Sexton

message Sorry to go OT -- for a monet -- but you're able to load documentation
at msdn2.microsoft.com?
Quite a few people can not load pages at msdn2 for many weeks now and
trying to figure out why.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP http://wikimapia.org/#y=43038073&x=-88043838&z=17&l=0&m=h

Hi Jon,

Your example code has been bothering me - it should work, however it
doesn't and I can't figure out why :p

But if you think that's weird, check out the following documentation I
found on MSDN:

"String.Intern Method"
http://msdn2.microsoft.com/en-us/library/system.string.intern.aspx

<quote>
Version Considerations
Starting with the .NET Framework version 2.0, there is a behavioral
change in the Intern method. In the following C# code sequence, the
variable str1 is assigned a reference to Empty, the variable str2 is
assigned the reference to Empty that is returned by the Intern method,
then the references contained in str1 and str2 are compared for
equality.

string str1 = String.Empty;
string str2 = String.Intern(String.Empty);
if ((object) str1) == ((object) str2) .

In the .NET Framework version 1.1, str1 and str2 are not equal, but
starting in the .NET Framework version 2.0, str1 and str2 are equal.
</quote>

Not in my test :|

string str1 = String.Empty;
string str2 = String.Intern(String.Empty);

// false
Console.WriteLine((object) str1 == (object) str2);

// false
Console.WriteLine((object) string.Empty == (object) "");

// true
Console.WriteLine((object) "" == (object) "");


Using Reflector I verified that string.Empty is assigned the empty
literal, "", in the class constructor (.cctor). I also verified,
through testing, that interning works across assemblies; so I'm
stumped here.

The earliest version of the framework that I have installed on my
machine is 2.0, verified in Add or Remove Programs. I also have 3.0
installed, if that makes a difference.

Anyone know what's going on here?

--
Dave Sexton

Morten Wennevik wrote:

"" is easy to write, easy to read, and near impossible to
misunderstand.
It will however create an object where String.Empty would not, but
the ""
object is reused throughout the lifespawn of your application so the
difference can therefore be ignored.

I was surprised to read that "" will create an object where
String.Empty will not - surely String.Empty is just a static field
that points to an interned "" constant?

Sure enough, Reflector shows that String.Empty is implemented as

class string
{
static readonly string Empty = "";
}

... yet the below code shows that while both "" and String.Empty are
interned, "" is not reference-equal to String.Empty. What gives? I
thought the intern table was maintained across assembly boundaries,
so
it shouldn't matter that String.Empty comes from mscorlib.dll while
""
comes from the app assembly.

//

using System;

namespace StringEmptyTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Object.ReferenceEquals(String.Empty, ""));
Console.WriteLine(String.IsInterned("") != null);
Console.WriteLine(String.IsInterned(String.Empty) != null);
Console.ReadLine();
}
}
}


--

.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top