There's an unofficial category of posts here that I call "Somebody told us string builders were more efficient, so we always use string builders, regardless of whether or not that actually makes sense," or STUSBWMESWAUSBROWONTAMS, for short. Probably should workshop that name some.
In any case, Adam sends us a new entry for that collection:
Public Function ReadFromFile(ByVal filename As String) As String
Dim sb As StringBuilder = New StringBuilder()
Dim str As String = String.Empty
Using sr As StreamReader = File.OpenText(filename)
str = sr.ReadToEnd()
sb.Append(str & Constants.vbLf)
End Using
Return sb.ToString()
End Function
This function takes a filename, opens a file, reads the entire contents of the file to a variable. It concatenates a line feed to the end of the string it just read, then appends all of that to a string builder. Finally, we return sb.ToString()
, taking the single string in the string builder, appending nothing to it, and returning the result.
I'd almost, almost forgive the code if it was more like:
str = sr.ReadToEnd()
sb.Append(str)
sb.Append(Constants.vbLf)
You're not actually gaining much from this, but…