0

I'm looking for a point in the right direction.

I'm in need of a List/Array/?? of a fixed size that I can add to the front, move all elements by one index and push the last element out.

Is it best to just code this behavior and if so are there any best practices to follow? or is there a something that can do this in .net core already?

I'm lacking the correct terminology for this kind of list so googling is difficult any help is appreciated.

Update Just to clarify, I will be listening to a websocket

  1. Data comes in
  2. Convert to object
  3. Add to list/array/stack/??

I envisage the class looking like


    List<foo> Stack

    AddToStack(Foo)
    {
       Stack.Add(Foo)
    }

    Get20SMA()
    {
        //Gets Last20 objects, does calculation on one property of each object and returns result
    }

    Get100SMA();
    {
        //Gets Last100 objects, does calculation on one property of each object and returns result
    }
NJKWilson
  • 1
  • 1
  • do you plan to read off the top or the bottom? – Hogan Mar 05 '21 at 19:27
  • 1
    What do you mean by 'move all elements by one index'? It sounds like you want a ring buffer/deque based on the other requirements. – Lee Mar 05 '21 at 19:30
  • If you don't need to access items in your object by `index` then you can use `Stack` and before calling `Push()` check the `Count` property, if it's reached your determined limit, call `Pop` first. – Ryan Wilson Mar 05 '21 at 19:33
  • There's also the `ConcurrentStack` for thread safety, if the above meets your requirements. [concurrentstack](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentstack-1?view=net-5.0), [stack](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1?view=net-5.0) – Ryan Wilson Mar 05 '21 at 19:41
  • 1
    The behavior described sounds like Queue not Stack. – Ben Voigt Mar 05 '21 at 20:27

0 Answers0