include::../why-dotnet.adoc[]

== How to fix it

=== Code examples

==== Noncompliant code example

[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Private fFoo As New List(Of String) From {"a", "b", "c"}
Private fBar As String() = {"a", "b", "c"}

Public ReadOnly Property Foo() As IEnumerable(Of String) ' Noncompliant: collection fFoo is copied
    Get
        Return fFoo.ToList()
    End Get
End Property

Public ReadOnly Property Bar() As IEnumerable(Of String) ' Noncompliant: array fBar is copied
    Get
        Return DirectCast(fBar.Clone(), String())
    End Get
End Property
----

==== Compliant solution

[source,vbnet,diff-id=1,diff-type=compliant]
----
Private fFoo As New List(Of String) From {"a", "b", "c"}
Private fBar As String() = {"a", "b", "c"}

Public Function GetFoo() As IEnumerable(Of String)
    Return fFoo.ToList()
End Function

Public Function GetBar() As IEnumerable(Of String)
    Return DirectCast(fBar.Clone(), String())
End Function
----

== Resources

=== Documentation

* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/properties[Properties (Visual Basic)]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/#fields-and-properties[Fields and properties]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/collections[Collections (Visual Basic)]

include::../rspecator.adoc[]
