Zum Inhalt springen

Cast or OfType to an incompatible type (CA2021)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key dotnet.codequality.incompatible-cast · Severity error · Category codequality

Microsoft analyzer CA2021. Flags Enumerable.Cast<T> or OfType<T> to a type no element can be. Cast throws InvalidCastException on the first element; OfType silently returns nothing.

IEnumerable<string> names = ids.Cast<string>(); // ids is IEnumerable<int>
IEnumerable<string> names = ids.Select(id => id.ToString());