rspec/rules/S2365/csharp/rule.adoc

44 lines
1.1 KiB
Plaintext
Raw Normal View History

2020-06-30 12:48:07 +02:00
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.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule detects calls to ``++ToList++``, ``++ToArray++`` and array ``++Clone++``.
2020-06-30 12:48:07 +02:00
== 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();
}
----