title: C# - Remove an item for a Linked List
body: <p>OK I'm having a problem, I just can't seem to get this to work, no matter what I try. The problem is everytime I write to Console I get an error: </p>
  
  <blockquote>
    <p>Argument 1: cannot convert from 'void' to 'bool'</p>
  </blockquote>
  
  <p>However, there is no boolean data from what I can see, bar the conditions.</p>
  
  <pre><code>    class Link
  {
      private int data;
      private Link next;
  
      public Link(int item) //constructor with an item
      {
          data = item;
          next = null;
      }
      public Link(int item, Link list) //constructor with item and list
      {
          data = item;
          next = list;
      }
  
      public int Data //property for data
      {
          set { this.data = value; }
          get { return this.data; }
      }
  
      public Link Next //property for next
      {
          set { this.next = value; }
          get { return this.next;}
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>All my other methods work i.e. Display item, count the number of items etc. But what seems to be the problem:</p>
  
  <pre><code>class LinkList
  {
      private Link list = null; //default value – empty list
  
      public void AddItem(int item) //add item to front of list
      {
          list = new Link(item, list);
      }
  
      public string DisplayItems() //write items to string and return
      {
          Link temp = list;
          string buffer = "";
          while (temp != null) // move one link and add head to the buffer
          {
              buffer += temp.Data + "";
              temp = temp.Next;
          }
          return buffer;
      }
  
      public int NumberOfItems() // returns number of items in list
      {
          Link temp = list;
          int count = 0;
          while (temp != null) // move one link and add 1 to count
          {
              count++;
              temp = temp.Next;
          }
          return count;
      }
  
  
  
      public void RemoveItem(int item)
      {
          Link current = list;
          Link previous = null;
  
          while (current != null)
          {
              if (current.Data == item)
              {
                  if (previous != null)
                  {
  
                      previous.Next = current.Next;
                      current = current.Next;
  
                  }
  
  
                  else
                  {
                      previous = current;
                      current = current.Next;
                      list = current;
                  }
              }
              else
                  previous = current;
                 current= current.Next;
          }
  
      }
  
  
  
  
  
      public bool IsPresent(int item)
      {
          Link temp = list;
          bool result = false;
          while (temp != null)
          {
              if (temp.Data == item)
              {
                  result = true;
                  break;
  
              }
  
              else
              {
                  temp = temp.Next;
  
              }
          }
          return result;
      }
  
  }
  </code></pre>
  
  <p>This is my program class I use to print to console-</p>
  
  <pre><code>    class Program
  {
      static void Main(string[] args)
      {
          LinkList testList = new LinkList();
          testList.AddItem(10);
          testList.AddItem(20);
          testList.AddItem(30);
          testList.AddItem(40);
          testList.AddItem(50);
          testList.AddItem(60);
  
          System.Console.WriteLine("Number of Items " + testList.NumberOfItems());
          System.Console.WriteLine("Display Items " + testList.DisplayItems());
          System.Console.WriteLine("10 is Present " + testList.IsPresent(10));
          System.Console.WriteLine("20 is Present " + testList.IsPresent(20));
          System.Console.WriteLine("30 is Present " + testList.IsPresent(30));
          System.Console.WriteLine("40 is Present " + testList.IsPresent(40));
          System.Console.WriteLine("50 is Present " + testList.IsPresent(50));
          System.Console.WriteLine("60 is Present " + testList.IsPresent(60));
          System.Console.WriteLine(testList.RemoveItem(10));
  
  
  
  
  
  
          System.Console.ReadLine();
          Console.ReadKey();
      }
  
  }
  </code></pre>
  
  <p>But the RemoveItem method giving me grief, could someone please possibly help me out? Thank you.</p>
  
  <p>This is the bit of code giving me a problem-</p>
  
  <pre><code>    public void RemoveItem(int item)
      {
          Link current = list;
          Link previous = null;
  
          while (current != null)
          {
              if (current.Data == item)
              {
                  if (previous != null)
                  {
  
                      previous.Next = current.Next;
                      current = current.Next;
  
                  }
  
  
                  else
                  {
                      previous = current;
                      current = current.Next;
                      list = current;
                  }
              }
              else
                  previous = current;
                 current= current.Next;
          }
  
      }
  </code></pre>
  
  <p>Thank you.</p>