Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.AsSpan ArrayTypeMismatchException

can smebody explain why .AsSpan thorws an ArrayTypeMismatchException while creating a new ReadOnlySpan doesn’t?

SomeMethod(Foo data) {
  IPoint[] tmp = (IPoint[])data.Points; // data.Points is OwnPoint[] (class OwnPoint : IPoint)
  //ReadOnlySpan<IPoint> pointsSpan = ((IPoint[])tmp).AsSpan(); //  System.ArrayTypeMismatchException: 'Attempted to access an element as a type incompatible with the array.'
  //ReadOnlySpan<IPoint> pointsSpan = tmp.AsSpan(); //  System.ArrayTypeMismatchException: 'Attempted to access an element as a type incompatible with the array.'
  ReadOnlySpan<IPoint> pointsSpan = new ReadOnlySpan<IPoint>(tmp);// works
  Bar(pointsSpan);
}

public void Bar(ReadOnlySpan<IPoint> pts) {
    // ...
}

what am I missing? thought that .AsSpan did the same as creating a new one.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s a question of type safety. As pointed by @canton7 Span<T> have check against type, while ReadOnlySpan<T> doesn’t. First one is mutable and it’s possible to assign some value to it. As long value have same type, everything is ok, but if value have different type (e.g. ((object[])new string[1])[0] = 1) array assignment throws ArrayTypeMismatchException so it won’t be possible to have values of different types inside array.

Span<T> does this check at instantiation time. I don’t know motivation behind this, maybe it’s performance.

ReadOnlySpan<T> doesn’t need this check since it’s not mutable.

You can read more about array casting in chapter 16 "Arrays" in Clr via C# by Jeffrey Richter

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading