Writing a class that represents a sorted list of filenames I have a question:


  Write a class that represents a sorted list of filenames. The filenames are sorted by most recently added first. Duplicates are not allowed.


And I wrote this solution:

public class RecentlyUsedList

{

    private readonly List&lt;string&gt; items;

    public RecentlyUsedList()

    {

        items = new List&lt;string&gt;();

    }

    public void Add(string newItem)

    {

        if (items.Contains(newItem))

        {

            int position = items.IndexOf(newItem);



            string existingItem = items[position];

            items.RemoveAt(position);

            items.Insert(0, existingItem);

        }

        else

        {

            items.Insert(0, newItem);

        }

    }

    public int Count

    {

        get

        {

            int size = items.Count;

            return size;

        }

    }

    public string this[int index]

    {

        get

        {

            int position = 0;

            foreach (string item in items)

            {

                if (position == index)

                    return item;

                ++position;

            }

            throw new ArgumentOutOfRangeException();

        }

    }

}


Now, other person asked me, do the code review of your own code and tell the answers of following questions:


  Three operations are required:
  
  
  How long is the list?
  Access filename at a given position
  Add a filename to the list; if it already exists in the list it gets moved to the top otherwise the new filename is simply prepended to the
  top.
  


I got confused and tried to look on Google but I didn't get much from there. What would be the best answers to these questions?
Empty lines as separation around code lines that are related are always a good idea. Your code has a little too much empty lines where it is not common in C# (or C/C++, Java, JavaScript):

public class RecentlyUsedList

{


This in fact decreases readability. Instead just write:

public class RecentlyUsedList
{





      private readonly List&lt;string&gt; items;

      public RecentlyUsedList()

      {

        items = new List&lt;string&gt;();

      }



Here the constructor is not necessary. Just do:

private readonly List&lt;string&gt; items = new List&lt;string&gt;();




Your Add(...) method can be simplified to this:

  public void Add(string newItem)
  {
    items.Remove(newItem);
    items.Insert(0, newItem);
  }


items.Remove(newItem) just returns false, if the item is not present, so it's safe to use in any case. There is no need to be concerned about the existing string because it is the same as the newItem.



public int Count... can be simplified to:

public int Count =&gt; items.Count;





  List&lt;string&gt; items


has an indexer it self, so you can use that when implementing the indexer:

  public string this[int index]
  {
    get
    {
      if (index &lt; 0 || index &gt;= items.Count)
      {
        throw new ArgumentOutOfRangeException();
      }
      return items[index];
    }
  }


Here I throw an exception if the index argument is out of range. You could let items handle that as well...

In fact your foreach-loop is potentially much slower than the List&lt;T&gt;[index] because you make a kind of search where List&lt;T&gt;[index] just performs a look up. So you hide an efficient behavior with a not so efficient one.