Skip to content

Commit

Permalink
Add diff and extend examples
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource committed Oct 22, 2024
1 parent f3fe6f1 commit 3eb4880
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions rules/S3878/csharp/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,47 @@ Creating an array or using a collection expression solely for the purpose of pas

==== Noncompliant code example

[source,csharp]
[source,csharp,diff-id=1,diff-type=noncompliant]
----
public void Base()
{
Method(new string[] { "s1", "s2" }); // Noncompliant: unnecessary
Method(new string[] { }); // Noncompliant
Method(["s3", "s4"]); // Noncompliant
Method(new string[12]); // Compliant
Method(new string[] { "s1", "s2" }); // Noncompliant: resolves to string[] overload
Method(new string[] { }); // Noncompliant: resolves to string[] overload
Method(["s3", "s4"]); // Noncompliant: resolves to ReadOnlySpan overload
Method(new string[12]); // Compliant: resolves to string[] overload
}
==== Compliant solution
public void Method(params string[] args)
{
// ...
}
public void Method(params ReadOnlySpan<string> args) // C# 13 params collections
{
// C# 13 params collection
}
----

=== Compliant solution

[source,csharp]
[source,csharp,diff-id=1,diff-type=compliant]
----
public void Base()
{
Method("s1", "s2");
Method();
Method("s3", "s4");
Method(new string[12]);
Method("s1", "s2"); // resolves to ReadOnlySpan overload
Method(); // resolves to ReadOnlySpan overload
Method("s3", "s4"); // resolves to ReadOnlySpan overload
Method(new string[12]); // resolves to string[] overload
}
public void Method(params string[] args)
{
// ...
// ..
}
public void Method(params ReadOnlySpan<string> args) // C# 13 params collections
{
// ..
}
----

Expand Down

0 comments on commit 3eb4880

Please sign in to comment.