Replies: 7 comments 6 replies
-
What are you proposing, taking backwards compatibility into account? |
Beta Was this translation helpful? Give feedback.
-
@JMM2 Maybe a warning about assigning a parent class to a subclass array? |
Beta Was this translation helpful? Give feedback.
-
@be5invis Roslyn analyzer or C# compiler warning? And what about contravariance? One errors on read, the other errors on write. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Both are okay. |
Beta Was this translation helpful? Give feedback.
-
Are you talking about code like this? class Base {}
class Derived : Base {}
…
Base[] b = new Base[1];
Derived[] d = b; Because that currently reports an error ("CS0266 Cannot implicitly convert type 'Base[]' to 'Derived[]'. An explicit conversion exists (are you missing a cast?)"). It compiles if you add a cast (i.e. |
Beta Was this translation helpful? Give feedback.
-
The language specification generally does not mandate warnings. It sounds more like a compiler or analyzer request, which would be more appropriate in the Roslyn repo. |
Beta Was this translation helpful? Give feedback.
-
This also prevents a bunch of possibilities of JIT optimization due to covariant arrays. Base[] array = new Derived[10];
array[0] = new Base(); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Γ ⊢ A <: B should NOT imply Γ ⊢ A[] <: B[], unless the array is immutable.
Reflect: dotnet/roslyn#4211
Beta Was this translation helpful? Give feedback.
All reactions