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.
Incorrect
Section titled “Incorrect”IEnumerable<string> names = ids.Cast<string>(); // ids is IEnumerable<int>Correct
Section titled “Correct”IEnumerable<string> names = ids.Select(id => id.ToString());