0

i need a collection and i am not sure which one to use. I have used List before but i need to also be sure about the specific position. If user views an item A i will ads it to the collection and if he sees another item B i will add that item on top of the first one and so on, but the limit number fot he items is 3 so i would remove the first item, also i need to be able to seriliaze the collection. I have tried Dictionary, but i could use XmlSerializer, so i have tried to use Lst<KeyValue<>> and now i am trying an array like this. Also had a look on Queue but i have found out that using XmlSerializer could also be an issue. Any suggestion for what collection i can use?

class Program
    {
        static void Main(string[] args)
        {
            string[] myObjArray = new string[3] ;

            if(myObjArray[0] == null)
            {
                myObjArray[0] = "article1";
                Console.WriteLine(myObjArray[0]);
            }
            if (myObjArray[1] == null )
            {
                myObjArray[1] = "article2";
                Console.WriteLine(myObjArray[1]);
            }
            if (myObjArray[2] == null)
            {
                myObjArray[2] = "article3";
                Console.WriteLine(myObjArray[2]);
            }

           var input =  Console.ReadLine();

            myObjArray[0] = input;
            Console.WriteLine(myObjArray[0]);


        }
    }

1 Answers1

3

You can use a List<Item> and use the Index as position and methods Insert and Delete to achieve your goal. If the position if encapsulated in the entity, you can create methods to manage it.

So when you add an item you will check if the count is over than the allowed and delete the first if nedeed.

[Serializable]
public class MyList
{

  private readonly List<Item> Items = new List<Item>();

  public int Count { get { return Items.Count; } }

  public int MaxCount { get; set; } = 0;

  public void Add(Item item)
  {
    if ( MaxCount > 0 && Items.Count >= MaxCount )
      Items.RemoveAt(0);
    Items.Add(item);
  }

  public void Insert(int index, Item item)
  {
    Items.Insert(index, item);
  }

  public int FindById(int id)
  {
    for ( int index = 0; index < Items.Count; index++ )
      if ( Items[index].Id == id )
        return index;
    return - 1;
  }

  // Add all over new methods and wrapping methods needed

}

This code use 0 to indicate that the max count is not considered, but if the list may not accept items, it can manage -1 for that, so 0 indicates that the list is closed.

Perhaps you can use a LinkedList that is searializable but you need to implement it for XML:

https://learn.microsoft.com/dotnet/api/system.collections.generic.linkedlist-1

How to Xml serialize a LinkedList?

So with that you can easily manage items as you wrote:

  • Add a cell between two cells.
  • Add a cell before a cell.
  • Add a cell after a cell.
  • Add a cell at the start.
  • Add a cell at the end.
  • Remove the first.
  • Remove the last.
  • And so on...

Hence you can add automatic delete the first cell if the count is over the allowed.

When should I use a List vs a LinkedList

LinkedList in C# - tutorialspoint.com

Linked Lists - dotnetcademy.net

C# | LinkedList Class - geeksforgeeks.org

Linked List Implementation in C# - geeksforgeeks.org