Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 11
    C# has unsafe array covariance because the .NET runtime does, and the .NET runtime has it because the designers of the runtime wanted it to be suitable for an implementation of Java, and Java has this dumb feature. It's one of the worst features of C#, in that it makes your programs both less robust and slower at the same time. Commented Jun 24 at 15:46
  • 1
    @EricLippert - Span<T> and Memory<T> don't have this anti-feature. So are they actually faster to work with than arrays, even when they are created from arrays? Commented Jun 24 at 20:40
  • 3
    @dbc: That was the idea, yes. The restrictions on how a span can be created and stored gives you most of the speed advantages of pointers while still maintaining a reasonable amount of type safety and memory safety. Commented Jun 24 at 22:18
  • Using a collection of object type seems too academic to be a real argument. Nobody in their right mind would actually write code that way (or at least, would choose it as the first option), when you have access to interfaces and compositional classing. Commented Jun 25 at 20:57
  • 1
    @K0D4: I have expanded on the example to show it is a general issue for arrays, not just something specific to object[]. Any time you have an array where the element type supports subtyping, you have this potential issue. The issue also affect interface subtyping. Commented Jul 2 at 6:08