Each string must have its own string data with how the String class is implemented.
You can create your own SubString structure that uses part of the string:
public struct SubString { private string _str; private int _offset, _len; public SubString(string str, int offset, int len) { _str = str; _offset = offset; _len = len; } public int Length { get { return _len; } } public char this[int index] { get { if (index < 0 || index > len) throw new IndexOutOfRangeException(); return _str[_offset + index]; } } public void WriteToStringBuilder(StringBuilder s) { s.Write(_str, _offset, _len); } public override string ToString() { return _str.Substring(_offset, _len); } }
You can use it in other ways, such as comparison, which can also be bypassed without extracting the string.
Guffa
source share