Search code examples
c#arrayswinformsdatapoint

C# how to "delete" first value of array, reorder array and set last value back at 0?


So I've got a Datapoint[] = new Datapoint[50] that I'm filling. As soon as the Datapoint array is full, how can I "delete" Datapoint[0] and set Datapoint[1] to Datapoint[0], Datapoint[2] to Datapoint[1]... etc. and make Datapoint[49] free again / to value 0/0?
I didn't had any success yet figuring it out since I don't wanna use a loop.

Thanks for help!


Solution

  • One option to consider would be to create a completely new array and assign it to your existing variable.

    The code would look something like:

    existingVariableName = existingVariableName
        .Skip(1)
        .Concat(new Datapoint[] { new Datapoint() })
        .ToArray();
    

    Skip(1) means to skip over the existing first element. Concat is used to add a new element at the end. ToArray materialises the LINQ query into the new resulting array.