Skip to main content
added 30 characters in body
Source Link
Robert Harvey
  • 200.1k
  • 55
  • 468
  • 679

c# - tryingTrying to understand strings better in C#

areAre these assertions correct?

  • string is immutable
  • string is reference type but behaves like value type

string is immutable string is reference type but behaves like value typeFor these code samples...

string x = "one"   

string x = "one" (creates string instance holding "one" in memory)

x = "two"   

x = "two" (destroys "one" instance and creates new string instance holding "two" in memory, even though it is using the same variable x)

If the above are correct, what happens in a string array when one index value changes?

string[] array = new string[2];

array[0] = "boo"; (string "boo" created and held in 0 index) array[1] = "shoo";

array[0] = "moo";

string[] array = new string[2];

array[0] = "boo";   (string "boo" created and held in 0 index)
array[1] = "shoo";

array[0] = "moo";  

QUESTION: doesDoes the last assignment create an entire new array to change boo to moo? My best "guess" is that the array holds pointers so that array[0] simply points to the new string instance that holds "moo". Is this correct? If not, could someone please clarify, thanks.

c# - trying to understand strings better

are these assertions correct?

string is immutable string is reference type but behaves like value type

string x = "one" (creates string instance holding "one" in memory)

x = "two" (destroys "one" instance and creates new string instance holding "two" in memory, even though it is using the same variable x)

If the above are correct, what happens in a string array when one index value changes?

string[] array = new string[2];

array[0] = "boo"; (string "boo" created and held in 0 index) array[1] = "shoo";

array[0] = "moo";

QUESTION: does the last assignment create an entire new array to change boo to moo? My best "guess" is that the array holds pointers so that array[0] simply points to the new string instance that holds "moo". Is this correct? If not, could someone please clarify, thanks.

Trying to understand strings better in C#

Are these assertions correct?

  • string is immutable
  • string is reference type but behaves like value type

For these code samples...

string x = "one"   

(creates string instance holding "one" in memory)

x = "two"   

(destroys "one" instance and creates new string instance holding "two" in memory, even though it is using the same variable x)

If the above are correct, what happens in a string array when one index value changes?

string[] array = new string[2];

array[0] = "boo";   (string "boo" created and held in 0 index)
array[1] = "shoo";

array[0] = "moo";  

Does the last assignment create an entire new array to change boo to moo? My best "guess" is that the array holds pointers so that array[0] simply points to the new string instance that holds "moo". Is this correct? If not, could someone please clarify, thanks.

Source Link
nanonerd
  • 189
  • 1
  • 6

immutable string in array, reference type vs value type

c# - trying to understand strings better

are these assertions correct?

string is immutable string is reference type but behaves like value type

string x = "one" (creates string instance holding "one" in memory)

x = "two" (destroys "one" instance and creates new string instance holding "two" in memory, even though it is using the same variable x)

If the above are correct, what happens in a string array when one index value changes?

string[] array = new string[2];

array[0] = "boo"; (string "boo" created and held in 0 index) array[1] = "shoo";

array[0] = "moo";

QUESTION: does the last assignment create an entire new array to change boo to moo? My best "guess" is that the array holds pointers so that array[0] simply points to the new string instance that holds "moo". Is this correct? If not, could someone please clarify, thanks.