C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: We use ReferenceEquals to see if two variables point to the same reference. String.Copy returns a separate, new string.
VB.NET program that uses String.Copy
Module Module1
Sub Main()
Dim value As String = "cat"
' Use String.Copy to copy a string.
Dim valueCopied As String = String.Copy(value)
' The copy has a different reference.
If Not ReferenceEquals(value, valueCopied) Then
Console.WriteLine("Not same reference")
End If
End Sub
End Module
Output
Not same reference
Here: We copy the first 3 characters in a string to a 3-element char array. We then use For-Each to display the array's contents.
For Each, ForTip: In VB.NET we use the last array index to indicate its length. So a 2 means a 3-element array.
ArrayVB.NET program that uses CopyTo
Module Module1
Sub Main()
Dim value As String = "dog"
' A 3-element char array.
Dim destination(2) As Char
' Copy string to the destination array.
value.CopyTo(0, destination, 0, 3)
' Loop over the resulting char array.
For Each letter As Char In destination
Console.WriteLine(letter)
Next
End Sub
End Module
Output
d
o
g