C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Immutable: A String cannot be directly changed. So when "Codex" is appended to the String, a whole new String is created.
StringsAnd: During String creation, the length value is stored. The String will thus always have the same length.
VB.NET program that uses Length property
Module Module1
Sub Main()
' Get length of string.
Dim value As String = "dotnet"
Dim length As Integer = value.Length
Console.WriteLine("{0}={1}", value, length)
' Append a string.
' ... Then get the length of the newly-created string.
value += "Codex"
length = value.Length
Console.WriteLine("{0}={1}", value, length)
End Sub
End Module
Output
dotnet=6
dotnetCodex=11
Important: In VB.NET, the value is stored, which makes getting the length of any string equally fast.
However: If the Length were to be computed each time, a loop over every character that tested against the bound Length would be slow.
And: The developer would have to cache the value. This code is not necessary in VB.NET.
Note: We run a benchmark that tests Length access in a for-loop. Using a "length" local does not help performance.