Most developers expect property access to be as efficient as field access. However, if a property returns a copy of an array or collection, it will be much slower than simple field access, contrary to the caller's likely expectations. Therefore, such properties should be refactored into methods so that callers are not surprised by the unexpectedly poor performance.


This rule detects calls to ``++ToList++``, ``++ToArray++`` and array ``++Clone++``.

== Noncompliant Code Example

----
private List<string> _foo = new List<string> { "a", "b", "c" };
public IEnumerable<string> Foo  // Noncompliant
{ 
    get
    {
        return _foo.ToList(); 
    }
}

private string[] _bar = new string[] { "a", "b", "c" };
public IEnumerable<string> Bar // Noncompliant
{ 
    get
    {
        return (string[])_bar.Clone(); 
    } 
}
----

== Compliant Solution

----
private List<string> _foo = new List<string> { "a", "b", "c" };
private string[] _bar = new string[] { "a", "b", "c" };

public IEnumerable<string> GetFoo()
{ 
    return _foo.ToList();
}

public IEnumerable<string> GetBar()
{
    return (string[])_bar.Clone();
}
----

ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]
