473,473 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

string immutability

rb
Hi

This is a sample code I am working on.

string a = "rb"
string b = a

Console.WriteLine(Object.ReferenceEquals(a,b))

In the above code, its printing true. I have a doubt on this output. When I say b = a, the system will create a new string called b and copy the value in a to b. Hence b and a should point to two new memory locations. Hence the output should be false, right

Can anybody clarify my doubt

Thanks
Nov 15 '05 #1
4 1526
rb,

This is not correct. Strings are reference types. When you declare b,
you are creating a new variable. When you assign a to b, then you are
copying the reference to a into the variable b. The references are copied,
not the actual object they point to. So, when you make the assignment, b is
pointing to the same thing a is.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"rb" <an*******@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
Hi,

This is a sample code I am working on.

string a = "rb";
string b = a;

Console.WriteLine(Object.ReferenceEquals(a,b));

In the above code, its printing true. I have a doubt on this output. When I say b = a, the system will create a new string called b and copy the value
in a to b. Hence b and a should point to two new memory locations. Hence the
output should be false, right.
Can anybody clarify my doubt?

Thanks

Nov 15 '05 #2
Immutability means when you MODIFY the string it will make a copy.

string b = a;

does not modify any value,

"rb" <an*******@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
Hi,

This is a sample code I am working on.

string a = "rb";
string b = a;

Console.WriteLine(Object.ReferenceEquals(a,b));

In the above code, its printing true. I have a doubt on this output. When I say b = a, the system will create a new string called b and copy the value
in a to b. Hence b and a should point to two new memory locations. Hence the
output should be false, right.
Can anybody clarify my doubt?

Thanks

Nov 15 '05 #3
Jason Wang <wa***@aegiscomgroup.com> wrote:
Immutability means when you MODIFY the string it will make a copy.


No it doesn't. It means you *can't* modify the string. No string
operations change it at all.

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

"Jason Wang" <wa***@aegiscomgroup.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Immutability means when you MODIFY the string it will make a copy.


I think that a clarification is needed regarding immutable.

A class is said to be inmutable when the programmer assure that none of the
methods/properties call will change the instance over which the call is
made, instead a new instance of the same class is created and returned on
those call that would modify the instance.

Also take a look that AFAIK there is no way to mark a type as immutable and
make this enforced by the compiler, therefore you have to trust the
programmer of the type.

Just my 2 cts. :)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 15 '05 #5

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

Similar topics

5
by: anthonyberet | last post by:
I am an abject newbie, so mock away (actually no-one ever does that in this group..) Anyway, I want to replace one character in a string, based in that character's position in the string. For...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
4
by: Diffident | last post by:
Hello All, I have a string of around 150 characters. string test = "asjhdkashdkjahsdjkhaskjdhaskjdhasjdhasd........."; //(upto 150 characters) I want to split the above string into multiple...
6
by: Diffident | last post by:
Dear Bruce, Can you please explain me how compiler would be able to optimize "+" for the below code? The reason why I did not want to use string concatenation was that I wanted it to be a...
14
by: Dan Aldean | last post by:
Hello, I have a file with CR/LF separated text. string.Trim() and string.Split() came very handy to process the content, but with the immutability the memory is badly managed. StringBuilder is...
9
by: amyxmas | last post by:
I am trying to write a routine which follows this logic, but not sure how to implement: <script> xxx = "... apple green yellow blue ..."; {right here I need to modify part of the string, so...
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
3
by: youtoo | last post by:
It has been extensively discussed the time complexity (quadratic) of string concatenation (due to string's immutability). But what is: == the time complexity of string indexing? Is it constant?...
0
by: Simon Brunning | last post by:
2008/8/25 Hendrik van Rooyen <mail@microcorp.co.za>: You can indeed use ctypes to modify the value of a string - see <http://tinyurl.com/5hcnwl>. You can use it to crash the OS, too. My advice...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
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...
0
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.