Introduction
We all know string concatenation is bad when working with SQL-statements because of the sql-injection that can occur. So we avoid that by using parameters. But string concatenation is bad in all your code because of the immutability of strings. You can avoid those bad things by using String.Format or StringBuilder.
But it ain’t all that simple why and when you want and need to use either of the two. After all readibility and maintainability are rarely a consideration for the two.
Here are some things to read.
- What’s the best string concatenation method using C#? on Stackoverflow
- More StringBuilder advice by Rico Mariani
- String Formatting in C# by SteveX
- String Formatting FAQ by SteveX
If you read the above blogs the need for Stringbuilder is mainly a speed thing. But it only helps much when you have enough concatenations and then some. It is still good practice to use the stringbuilder if you need to concatenate several strings together. And in the programming world 99% of string need concatenating.
String.Format
Now why would I prefer for you to use String.Format over a normal concatenation?
String concatenation
Dim s1 as String = "1"
Dim s2 as String = "1"
Dim s3 as String = "1"
Dim s4 as String = "1"
Dim s5 as String
s5 = s1 & " " & s2 & " " & s3 & " " & s4```
String.Format
```vbnet
Dim s1 as String = "1"
Dim s2 as String = "1"
Dim s3 as String = "1"
Dim s4 as String = "1"
Dim s5 as String
s5 = string.Format("{0} {1} {2} {3}",s1, s2 ,s3 ,s4)
The result of the two statements is the same and we don’t really do premature optimization so we don’t care which one of the two is faster. But the String.Format one just seems to be better at telling me what it does. So I prefer that one.
Resharper
Now that I know I want my code to use either stringbuilder, String.Format or parameters (in the case of sql-statements) I must make sure that my fellow programmers (being me) don’t mess it up. And that’s where resharper and the custom patterns come in.
To make a custom pattern you go to the Resharper Options menu.
And then you need to add a pattern. For which I got a little help.
Like I did the above. You can find more details on how to this at Hadi’s place.
So I’m looking for &
with a string in front and behind it. str1 and str2 are placeholders of type expression and they should match System.String or derived type of that.
Now when I do a search it will find all the concatenations in my code.
But that is not all. As you can see above I also set the severity level to warning, so the resharper code inspection thing will now also show a blue squigly line and give me the message I typed in.
And even better is that you can do Alt+Enter and have it fix it for you by choosing “Use format string for all arguments”.
and it goes from this.
To this.
In a matter of a second.
Conclusion
There are so many things we need to know and do but from time to time it is best to have tools to help us do our jobs better and faster. So use those tools. Of course learning those tools takes time and effort.