Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Property | Value |
---|---|
Rule ID | CA2009 |
Title | Do not call ToImmutableCollection on an ImmutableCollection value |
Category | Reliability |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | As suggestion |
ToImmutable
method was unnecessarily called on an immutable collection from System.Collections.Immutable namespace.
System.Collections.Immutable namespace contains types that define immutable collections. This rule analyzes the following immutable collection types:
These types define extension methods that create a new immutable collection from an existing IEnumerable<T> collection.
These extension methods are designed to convert a mutable collection to an immutable collection. However, the caller might accidentally pass in an immutable collection as input to these methods. This can represent a performance and/or a functional issue.
To fix violations, remove the redundant ToImmutable
call on an immutable collection. For example, the following two code snippets show a violation of the rule and how to fix them:
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
public class C
{
public void M(IEnumerable<int> collection, ImmutableArray<int> immutableArray)
{
// This is fine.
M2(collection.ToImmutableArray());
// This leads to CA2009.
M2(immutableArray.ToImmutableArray());
}
private void M2(ImmutableArray<int> immutableArray)
{
Console.WriteLine(immutableArray.Length);
}
}
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
public class C
{
public void M(IEnumerable<int> collection, ImmutableArray<int> immutableArray)
{
// This is fine.
M2(collection.ToImmutableArray());
// This is now fine.
M2(immutableArray);
}
private void M2(ImmutableArray<int> immutableArray)
{
Console.WriteLine(immutableArray.Length);
}
}
Tip
A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press Ctrl+. (period). Choose Remove redundant call from the list of options that's presented.
Do not suppress violations from this rule, unless you're not concerned about the performance impact from unnecessary allocations of immutable collections.
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA2009
// The code that's violating the rule is on this line.
#pragma warning restore CA2009
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA2009.severity = none
For more information, see How to suppress code analysis warnings.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now