Reputation: 23
Everyone (or everyone who has been taught) knows that StringBuilder is the way to go for dynamically building a string with many concatenations.
Due to (I must confess) mostly laziness, if I am doing a few concatenations (say, around 3), I will use a plain old String. I guess I just can't handle having that extra instantiation and toString.
Is using a plain old String in this scenario poor practice? Is there any extra overhead for using a StringBuilder vs. a String in this context, or would StringBuilder still be more efficient?
Specific Examples:
Dim PractitionerName As String = CurrentApplicant.FirstName
If CurrentApplicant.Demographics.MiddleInitial <> "" Then
PractitionerName &= " " & CurrentApplicant.Demographics.MiddleInitial
End If
If CurrentApplicant.LastName <> "" Then
PractitionerName &= " " & CurrentApplicant.LastName
End If
Dim Endorsements As String = ""
For Each Endorsement As IowaLicensure.LicenseEndorsement In PractitionerLicense.LicenseEndorsements
Endorsements &= Endorsement.EndorsementType.Description & VbCrLf
Next
Upvotes: 1
Views: 63
Reputation: 38915
The devil is in the details, but you may be able to use something like this in some cases:
If bePolite Then
Return String.Format("{0} {1} {2} {3}", Title, FirstN, MiddleI, LastN)
' "Doctor Jonas K Salk"
Else
Return String.Format("{0}, {1}", LastN, FirstN)
' "Jones, Sally"
End If
The For Each scenario is trickier but something similar can sometimes be done from one of the underlying classes. In most cases, not much will likely be saved unless there is a larger loop iterating.
Upvotes: 2