views:

365

answers:

3
A: 
Konrad Rudolph
@Konrad: My requirement is that I return fragments of the original array from a function. ArraySegment(arr, 0, 2).Array returns the original FULL array and not an array fragment
Vyas Bharghava
Why is that a problem? It returns a *wrapper* that behaves as if it were only part of the old array. That's the whole point of a wrapper.
Konrad Rudolph
My method signature is this: byte [] CalculateXXX(byte [] message, byte[] key); I get a final byte array of length X+10. Only need length X. I can't return ArraySegment [as it's not an Array].
Vyas Bharghava
The solution is to change your method signature! Use either `IEnumerable<byte>` or `IList<byte>`, according to need. Arrays as explicit types should be completely banned from your method signatures (for exactly the reason to avoid the kind of problems you've got now).
Konrad Rudolph
Thanks for the reply, Konrad. Even if I change the signature to IEnumerable<byte>, ArraySegment does not implement IEnumerable. Secondly, it would mean a closure [retaining the original array]. Manipulation of the ArraySegment would alter the original array if I were to return a field
Vyas Bharghava
Hey, No issues Konrad.. You're trying to help and I really appreciate it... Thanks :)
Vyas Bharghava
A: 

Define better. What is the downside with ArraySegment? What problem are you having that it doesn't solve?


Edit: Ok, I now understand your point of view, but this is wrong. It might be a bug in the sense that you feel it should do more, but it does exactly what it is supposed to do. It allows you to pass information to a piece of code about an array you wish to use, and which portion of the array to use.

It doesn't provide IEnumerable or anything that gives you a narrow view of the array, ie. x[0] doesn't give you the first element of the segment, nor can you foreach over it.

So yes, it's rather useless on its own, but you wouldn't be able to get something that is an array at heart, and yet is also a segment of a larger array.

You could easily make your own collection-like class that references an array and uses the array as storage, and provides indexing, enumeration, etc.

But that's not what this structure does. I guess the main problem here is that they made you expect more from it through its name.

Lasse V. Karlsen
Lassevk, read the comments to my answer. ArraySegment seems to be really, really useless.
Konrad Rudolph
@Lassevk: Hm.. you know this is turning into a discussion of whether ArraySegment does as it's name suggests or not... Agreed. ArraySegment could be very useful in threaded scenarios and it may not be intended, by design, to do what I need. I don't care to use ArraySegment per se... Any other way?
Vyas Bharghava
A: 

Just another side-effect of poor iterator design in C# btw. There are many instances similar (just as 'lame') where it would just plain good design to be able to pass or point or control segments (aka concept called range) without all the archane shickanery.. copy semantics or not, array is also a well-defined concept.

Just use C++. :-)

rama-jka toti