1

There i try to specify bounds of an array:

public string[] scaleChanged()
{
    int j =0;
    for(int i=0;i<42;i++)
    {
        if(MinScale == ItemScale[i]) //MinScale is the min value I want to use to start array
        {
            ItemsScale[j] = MinScale; //ItemScal is an array of 42 string
            for(int h =0; h<42-i; h++)
            {
                ItemScale[j+1] = ItemScale[i];
                j++;
                if(ItemScale[j] == MaxScale) //MaxScale is my max value I want to use for stop my array
                {
                    return ItemScale[ ???MinScale to MaxScale];
                }
            }
        }
    }
}

So I recover 2 value from a server which allow me to specify bounds of my array.

So I try to define a new array with this two values as bounds.I precise this "two values" are always declared anywhere in my ItemScale array (that is why i use comparaison).

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    Are you asking how to get a subset of an array? See [here](https://stackoverflow.com/questions/1792470/subset-of-array-in-c-sharp) for that. If you're just trying to create an array with an arbitrary lower and upper bound then this is not possible. You can only define the length of the array, and the lower bound is always 0. – ProgrammingLlama Aug 28 '20 at 07:40
  • @John - I believe they're asking how to set the range of the array, for instance having numbering start at `x` and go on to `y`. – Paul Aug 28 '20 at 07:43
  • 1
    You can create your own type with an array and an offset. – Alessandro D'Andria Aug 28 '20 at 07:46
  • C# doesn't have that facility. Arrays have a length and are zero based. You could write a class to implement it, but that's a bit of effort for what's basically syntactic sugar. – Tony Hopkinson Aug 28 '20 at 07:49
  • @John ok so the solution is to determine the number of values between my MinScale and my MaxScale. With this length i can determine my new array. – LaserProject Aug 28 '20 at 07:52
  • This is completely unclear. Can you perhaps explain it a different way? – TheGeneral Aug 28 '20 at 07:56
  • @MichaelRandall Create an array which start at x (define by me) and go on to y (define by me). – LaserProject Aug 28 '20 at 08:03
  • Then @MarcGravell has you covered, in just about every conceivable way – TheGeneral Aug 28 '20 at 08:03
  • `ItemScale.SkipWhile( x => x < MinScale).TakeWhile( x => x <= MaxScale).ToArray()` – John Wu Aug 28 '20 at 08:36

1 Answers1

3

If really depends what you are trying to do here. The bounds of an array are fixed at creation, and in the case of a vector (string[] is a vector), it is always zero-based. If you want an actual array, then you'll need to copy out the sub-range into a second array - just new the array of the correct size, and Array.Copy the element range you want - i.e. Array.Copy(source, startIndex, destination, 0, count);. However, there are ways to represent a range without copying:

  • if the receiver just needs to iterate the data (rather than access it by index), an IEnumerable<T> - i.e. return source.Skip(firstIndex).Take(count);
  • Span<T> or ReadOnlySpan<T>, i.e. return new Span<string>(source, firstIndex, count) - a "span" works much like an array, and doesn't require any copying, and is allocation-free; the offset etc is applied appropriately; note that once you have a span, .Slice(...) creates smaller and smaller sub-sections inside that span, again without any copying or allocations
    • one nice thing here is that you can use the span approach as a simpler way of dealing with creating a new array if you want - once you have a [ReadOnly]Span<T>, you can use .ToArray() to create a new array with those contents
  • Memory<T> or ReadOnlyMemory<T> is effectively "I can give you a span when you want one" - because you can't store a "span" as a field (it is only legal on the stack), but you can store a "memory"
  • ArraySegment<T> is an older metaphor for expressing an array with offset and count; it relies on the caller doing everything correctly
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900