473,625 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String constructor returning interned string?

I've just noticed something rather odd and disturbing. The following
code displays "True":

using System;

class Test
{
public static void Main(string[] args)
{
string x = new string ("".ToCharArray ());
string y = new string ("".ToCharArray ());
Console.WriteLi ne (object.Referen ceEquals (x, y));
}
}

In other words, new string(...) is *not* returning a new string
reference.

This worries me - not so much for the specific example, but for the
precedent set. What other new ... expressions might return non-new
references? This could have significant implications in multi-
threading, where you may rely on two references being different for
locking purposes.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #1
4 1656

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
I've just noticed something rather odd and disturbing. The following
code displays "True":

using System;

class Test
{
public static void Main(string[] args)
{
string x = new string ("".ToCharArray ());
string y = new string ("".ToCharArray ());
Console.WriteLi ne (object.Referen ceEquals (x, y));
}
}

In other words, new string(...) is *not* returning a new string
reference.

This worries me - not so much for the specific example, but for the
precedent set. What other new ... expressions might return non-new
references? This could have significant implications in multi-
threading, where you may rely on two references being different for
locking purposes.

This is odd...quite honestly. However, have you checked to make sure the JIT
isn't making some kind of very clever optimzation here? Perhaps realizing
your creating two strings from the same source(the char array of an already
interned string), and not creating the new object but instead setting both x
& y to the same reference?
Otherwise this is a quite disturbing find, indeed.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #2
Daniel O'Connell <onyxkirx@--NOSPAM--comcast.net> wrote:
This worries me - not so much for the specific example, but for the
precedent set. What other new ... expressions might return non-new
references? This could have significant implications in multi-
threading, where you may rely on two references being different for
locking purposes.
This is odd...quite honestly. However, have you checked to make sure the JIT
isn't making some kind of very clever optimzation here? Perhaps realizing
your creating two strings from the same source(the char array of an already
interned string), and not creating the new object but instead setting both x
& y to the same reference?


It only happens with the empty string, as far as I can see.
Otherwise this is a quite disturbing find, indeed.


I think it's disturbing either way - basically, if you rely on the new
operator always returning a previously unknown reference, you've got
problems.

However, I've looked at the String(char[]) docs, and the remarks say:

<quote>
If value is a null reference (Nothing in Visual Basic) or contains no
element, an Empty instance is initialized.
</quote>

I suspect if I hadn't known what that meant beforehand (due to seeing
this) I wouldn't have understood it.

I'd have thought this would actually take more work, and that there
wouldn't really be that much benefit in it. I just wonder where else
this might be lurking...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
There's more on .NET's string interning over on Chris Brumme's blog:

http://blogs.gotdotnet.com/cbrumme/P...3-3d7a0dbba270

He even has some example code snippets to illustrate how it can bite you
when calling into unmanaged code.

/kel

Jon Skeet wrote:
Daniel O'Connell <onyxkirx@--NOSPAM--comcast.net> wrote:
This worries me - not so much for the specific example, but for the
precedent set. What other new ... expressions might return non-new
references ? This could have significant implications in multi-
threading, where you may rely on two references being different for
locking purposes.


This is odd...quite honestly. However, have you checked to make sure the JIT
isn't making some kind of very clever optimzation here? Perhaps realizing
your creating two strings from the same source(the char array of an already
interned string), and not creating the new object but instead setting both x
& y to the same reference?

It only happens with the empty string, as far as I can see.

Otherwise this is a quite disturbing find, indeed.

I think it's disturbing either way - basically, if you rely on the new
operator always returning a previously unknown reference, you've got
problems.

However, I've looked at the String(char[]) docs, and the remarks say:

<quote>
If value is a null reference (Nothing in Visual Basic) or contains no
element, an Empty instance is initialized.
</quote>

I suspect if I hadn't known what that meant beforehand (due to seeing
this) I wouldn't have understood it.

I'd have thought this would actually take more work, and that there
wouldn't really be that much benefit in it. I just wonder where else
this might be lurking...


Nov 15 '05 #4
Hi, Jon,
Null and Empty string are the only cases.
This is to avoid too many instances of empty strings in CLR.
But I agree we probably should always create new strings in new String(...)

Gang Peng
[MS]

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Daniel O'Connell <onyxkirx@--NOSPAM--comcast.net> wrote:
This worries me - not so much for the specific example, but for the
precedent set. What other new ... expressions might return non-new
references? This could have significant implications in multi-
threading, where you may rely on two references being different for
locking purposes.


This is odd...quite honestly. However, have you checked to make sure the JIT isn't making some kind of very clever optimzation here? Perhaps realizing your creating two strings from the same source(the char array of an already interned string), and not creating the new object but instead setting both x & y to the same reference?


It only happens with the empty string, as far as I can see.
Otherwise this is a quite disturbing find, indeed.


I think it's disturbing either way - basically, if you rely on the new
operator always returning a previously unknown reference, you've got
problems.

However, I've looked at the String(char[]) docs, and the remarks say:

<quote>
If value is a null reference (Nothing in Visual Basic) or contains no
element, an Empty instance is initialized.
</quote>

I suspect if I hadn't known what that meant beforehand (due to seeing
this) I wouldn't have understood it.

I'd have thought this would actually take more work, and that there
wouldn't really be that much benefit in it. I just wonder where else
this might be lurking...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2489
by: Byron Morgan | last post by:
Anyone run into this before? I have a python app that has been reliable, running for days on end without a crash. Suddenly, It repeatedly crashes with the following message: "Fatal Python error: Inconsistent interned string state. abnormal program termination" Some background:
6
2824
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel uneasy leaving it hanging in memory indefinitely (especially in case when string is Interned). So at leats for the case when string is not interned i propose:
11
1814
by: Zeng | last post by:
Hello, Is it true that in C# we can't modify directly a character in a string? I can't find the method anywhere. I would expect something like this should work,but it doesn't string myStr = "blah'; myStr = 'e';
7
1392
by: Dale | last post by:
A year or two ago, I read an article on Microsoft's MSDN or Patterns and Practices site about application optimization when using strings. Some of the recommendations were: use string.Empty rather than "", use string.Compare rather than myString == myOtherString, etc., etc. I have been searching for days now for that article or those performance guidelines and can't find it. Does anyone know where that article, or any
34
2641
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ____________ THE OVERVIEW I don't remember where I picked it up, but I remember reading years ago that the simple, obvious Python approach for string concatenation: x = "a" + "b"
26
2772
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
35
3651
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString = string.Empty; // do this string myString; // do not do this Questions 1. Is that a good rule? 2. If so, why? If not, why not?
3
1958
by: Sin Jeong-hun | last post by:
If I use something like this, string html = "<h1>C# is great</h1>"; Console.WriteLine(html.Replace("&lt;","<").Replace("&gt;",">")); Does this recreate new string objects two times, even though it has nothing to replace, and it is OK to return the same string object? I know, regular expression is a more delicate way to do this, but it always is kind of too complicated to me.
34
3528
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical difference?
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7188
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.