Do not call ToImmutableCollection on an ImmutableCollection value
Do not call ToImmutableCollection on an ImmutableCollection value
Microsoft docsDescription
System.Collections.Immutable namespace contains types that define immutable collections. This rule analyzes the following immutable collection types:
- System.Collections.Immutable.ImmutableArray`1
- System.Collections.Immutable.ImmutableList`1
- System.Collections.Immutable.ImmutableHashSet`1
- System.Collections.Immutable.ImmutableSortedSet`1
- System.Collections.Immutable.ImmutableDictionary`2
- System.Collections.Immutable.ImmutableSortedDictionary`2
These types define extension methods that create a new immutable collection from an existing System.Collections.Generic.IEnumerable`1 collection.
- System.Collections.Immutable.ImmutableArray`1 defines System.Collections.Immutable.ImmutableArray.ToImmutableArray.
- System.Collections.Immutable.ImmutableList`1 defines System.Collections.Immutable.ImmutableList.ToImmutableList.
- System.Collections.Immutable.ImmutableHashSet`1 defines System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet.
- System.Collections.Immutable.ImmutableSortedSet`1 defines System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet.
- System.Collections.Immutable.ImmutableDictionary`2 defines System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary.
- System.Collections.Immutable.ImmutableSortedDictionary`2 defines System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary.
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.
- Performance issue: Unnecessary boxing, unboxing, and/or runtime type checks on an immutable collection.
- Potential functional issue: Caller assumed to be operating on a mutable collection, when it actually had an immutable collection.
Cause
ToImmutable method was unnecessarily called on an immutable collection from System.Collections.Immutable namespace.
How to fix violations
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: A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press <kbd>Ctrl</kbd>+<kbd>.</kbd> (period). Choose Remove redundant call from the list of options that's presented. !Code fix for CA2009 - Do not call ToImmutableCollection on an ImmutableCollection value
Example
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);
}
}When to suppress
Do not suppress violations from this rule, unless you're not concerned about the performance impact from unnecessary allocations of immutable collections.