Trivial I know, but just interested
I’ve got a stringbuilder variable, that I want to return the contents of, but if it’s empty I want to return ‘|’, so is it best to use stringbuilder.tostring in the compare statement e.g
If lReturnStringBuilder.ToString = String.Empty Then lReturnStringBuilder.Append('|') End If return lreturnStringBuilder.tostring
or is it best to convert it to a string, and compare that, even though that means loading up a new variable and the allocating string space for that e.g
Dim lString as string = lReturnStringBuilder.ToString if lString = string.empty then lstring = '|' end if return lString
This is the sort of micro-optimisation that you really just don’t need to worry about. However, I will post what I would think is most elegant (and efficient) way of doing this anyway:
This saves converting an empty StringBuilder to a string unnecessarily (or then calling Append, which is definitely not required). Note the use of the inline If statement (VB 9.0), which does not evaluate both statements in any one case, as it is a language construct and not a function (exactly equivalent to a normal If statement with variable assignments).