Warning when an cast in foreach loop is necessary #7765
-
TL;DR: I think it would be nice to issue a compiler warning, when the Long version: As explained in the language specification, the loop foreach (V v in x) «embedded_statement» is transformed into {
E e = ((C)(x)).GetEnumerator();
try
{
while (e.MoveNext())
{
V v = (V)(T)e.Current;
«embedded_statement»
}
}
finally
{
... // Dispose e
}
} As you can see, there is a deliberate, explicit cast (which is also the reason why I'm not to much of an expert, but I think the compiler should be able to statically check whether the explicit casts involved are even truly necessary. (Maybe this assumption is wrong, in which case I stand corrected.) If so, I suggest to emit a warning, stating that the (This suggestion was spawned from https://stackoverflow.com/a/77653928/2473926.) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Duplicate of dotnet/runtime#48529. There's a lot of good discussions there on why the request was not approved. |
Beta Was this translation helpful? Give feedback.
Duplicate of dotnet/runtime#48529. There's a lot of good discussions there on why the request was not approved.