You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Purpose: Simplify array slicing. I know this is done in ArraySegment. I'm not sure as to why a roundabout method (ie. by switching types entirely) is used. As much as it can be done simply on the side, array slicing is a very useful feature to have.
The problem with ArraySegment is that it converts to a completely separate type, only to have to be converted back to an array.
To put it simply, C#'s implementation of array slicing is very counter-intuitive. And something as essential as array slicing shouldn't have such a complex implementation.
Proposed API
namespace System.Collections.Generic
{
public class Array<T> {
...
+ public static Array<T> Slice(int start, int end);+ public static Array<T> this[int start, int end] => Slice(start, end);
...
}
}
However, as straightforward as this is, having it as a native part of the language would be very convenient, since array slicing occurs often. Additional note: it may also be implemented as part of IEnumerable.
Python offers simple notation for this too, and I'm not sure of other languages that provide simple notation for slicing arrays.
I looked into ArraySegment, but again, switching to a separate type entirely for a simple task is inconvenient. Not even having a method for slicing at all makes little sense. I believe there's a variant of the Array<T>.Copy method that pulls it off, but for something as simple as a slice, it requires digging through several different call variations to get the right one, which again, seems unnecessary for such an essential programming feature.
Again, the this[int start, int end] is an optional simplification. But a Slice(int start, int end) method would be well appreciated.
Risks
If others have already defined the this[int, int] to implement a slice or something else, or a slice method of their own, it'll be overridden code. Besides that, there's obviously the IndexOutOfRange exception that might occur. I can't think of any risks other than that that wouldn't be present otherwise.
Thinking about risks of your own code is always a little difficult though. So feel free to mention anything else I haven't thought of.
The text was updated successfully, but these errors were encountered:
If you do slice array often, then you should use Memory or Span everywhere. I know there are many api not designed for spans, but arrays are not designed for slicing, neither. ArraySegment was a historical design. It's covered by Memory.
Background and Motivation
Purpose: Simplify array slicing. I know this is done in
ArraySegment
. I'm not sure as to why a roundabout method (ie. by switching types entirely) is used. As much as it can be done simply on the side, array slicing is a very useful feature to have.The problem with
ArraySegment
is that it converts to a completely separate type, only to have to be converted back to an array.To put it simply, C#'s implementation of array slicing is very counter-intuitive. And something as essential as array slicing shouldn't have such a complex implementation.
Proposed API
Usage Examples
Alternative Designs
As I said, this is straightforward to implement on your own, as it simply requires defining the method:
However, as straightforward as this is, having it as a native part of the language would be very convenient, since array slicing occurs often. Additional note: it may also be implemented as part of
IEnumerable
.Python offers simple notation for this too, and I'm not sure of other languages that provide simple notation for slicing arrays.
I looked into
ArraySegment
, but again, switching to a separate type entirely for a simple task is inconvenient. Not even having a method for slicing at all makes little sense. I believe there's a variant of theArray<T>.Copy
method that pulls it off, but for something as simple as a slice, it requires digging through several different call variations to get the right one, which again, seems unnecessary for such an essential programming feature.Again, the
this[int start, int end]
is an optional simplification. But aSlice(int start, int end)
method would be well appreciated.Risks
If others have already defined the
this[int, int]
to implement a slice or something else, or aslice
method of their own, it'll be overridden code. Besides that, there's obviously theIndexOutOfRange
exception that might occur. I can't think of any risks other than that that wouldn't be present otherwise.Thinking about risks of your own code is always a little difficult though. So feel free to mention anything else I haven't thought of.
The text was updated successfully, but these errors were encountered: