Assuming I have the following strings:
string str1 = "Hello World!";
string str2 = str1.SubString(6, 5); // "World"
I am hoping that in the above example str2 does not copy “World”, but simply ends up being a new string that points to the same memory space only that it starts with an offset of 6 and a length of 5.
In actuality I am dealing with some potentially very long strings and am interested in how this works behind the scenes for performance reasons. I am not familiar enaugh with IL to look into this.
It’s a new string.
Strings, in .NET, are always immutable. Whenever you generate a new string via a method, including Substring, it will construct the new string in memory. The only time you share references to the same data in strings in .NET is if you explicitly assign a string variable to another string (in which its copying the reference), or if you work with string constants, which are typically interned. If you know your string is going to share a value with an interned string (constant/literal from your code), you can retrieve the “shared” copy via String.Intern.
This is a good thing, btw – In order to do what you were describing, every string would require a reference (to the string data), as well as an offset + length. Right now, they only require a reference to the string data.
This would dramatically increase the size of strings in general, throughout the framework.