Span<T> usage examples
A human by nature cannot fully understand the purpose of a certain instrument until he or she gets some experience. So, let’s turn to some examples.
ValueStringBuilder
One of the most interesting examples in respect to algorithms is the ValueStringBuilder
type. However, it is buried deep inside mscorlib and marked with the internal
modifier as many other very interesting data types. This means we would not find this remarkable instrument for optimization if we haven’t researched the mscorlib source code.
What is the main disadvantage of the StringBuilder
system type? Its main drawback is the type and its basis — it is a reference type and is based on char[]
, i.e. a character array. At least, this means two things: we use the heap (though not much) anyway and increase the chances to miss the CPU cash.
Another issue with StringBuilder
that I faced is the construction of small strings, that is when the resulting string must be short e.g. less than 100 characters. Short formatting raises issues on performance.
This chapter was translated from Russian jointly by author and by professional translators. You can help us with translation from Russian or English into any other language, primarily into Chinese or German.
Also, if you want thank us, the best way you can do that is to give us a star on github or to fork repository github/sidristij/dotnetbook.