2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-05-25 14:18:12 +02:00
Method for creating empty arrays ``++Array.Empty<TElement>()++`` was introduced in .NET 4.6 to optimize object instantiation and memory allocation. It also improves code readability by making developer's intent more explicit. This new method should be preferred over empty array declaration.
2020-12-21 15:38:52 +01:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-12-21 15:38:52 +01:00
----
public void Method()
{
var zero_length = new int[0]; // Noncompliant
var empty_array = new string[] { }; // Noncompliant
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-12-21 15:38:52 +01:00
----
public void Method()
{
var zero_length = Array.Empty<int>();
var empty_array = Array.Empty<string>();
}
----
include::../see.adoc[]
2023-06-22 10:38:01 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Replace this empty array with Array.Empty<TElement>().
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]