I have this very simple array which I want to be able to move around some items in. Are there any built in tools in c# to do this? If not, du you have any suggestion in how to do it.
In example
var smallArray = new string[4];
smallArray[0] = "a";
smallArray[1] = "b";
smallArray[2] = "c";
smallArray[3] = "d";
And lets say I want to (programmatically) swap index 2 and 0, creating
smallArray[0] = "c";
smallArray[1] = "a";
smallArray[2] = "b";
smallArray[3] = "d";
Thanks.
EDIT: Okay, now you’ve changed the example, there’s nothing built-in – and it would actually be a bit of a pain to write… you’d need to consider cases where you’re moving it “up” and where you’re moving it “down”, for example. You’d want unit tests, but I think this should do it…
You should probably consider using a
List<T>instead of an array, which allows you to insert and remove at particular indexes. Those two operations will be more expensive than only copying the relevant section, but it’ll be a lot more readable.