10 Replies
Strings are reference types. However, you are confusing what that means here
str1
is a reference. When you reassign it, you're not changing other references to that same value
You never changed what str2
is ("string 2"), so it never changes
what about this case
str1
is pointing to str2
and str2
is changed to "BLAHH"
but it doesnt change str1
you're misunderstanding
str2 = "BLAHH"
isn't changing the value of the string str2
references, it's changing the reference to an entirely new string. so the contents of the string that str1
references never changes
C# strings are immutableNo.
str1
is pointing to the same string that str2
is pointing to. Then, you reassign str2
to point to a new string. That doesn't affect what str1
is pointing toooh
this immutability is also why people often say string is a reference type that behaves like a value type. This is how I remember it
This has nothing to do with string immutability, and all reference types have exactly the same behavior as these examples
thats when youre comparing strings
right, sorry
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.