Performance issues that need to be considered when programming in C#
Nowadays, C# is one of the most commonly used programming languages all over the world, so good tips about C# programming are fundamental for the design of good architectures that satisfy the most common non-functional requirements.
The following sections mention a few simple but effective tips – the associated code samples are available in the GitHub repository of this book.
String concatenation
This is a classic one! A naive concatenation of strings with the +
string operator may cause serious performance issues since each time two strings are concatenated, their contents are copied into a new string.
So, if we concatenate, for instance, 10 strings that have an average length of 100, the first operation has a cost of 200, the second one has a cost of 200+100=300, the third one has a cost of 300+100=400, and so on. It is not difficult to convince yourself that the overall cost grows like m...