Search code examples
c#listprivateencapsulation

Clear property-(public) List without clearing the private List


I'm new to C# (first post) and currently working on a problem regarding "encapsulation". Basically I have two classes like this (simplified):

class Myclass
    {

        private List<X> _list;
        public List<X> list
        {
            get
            {
                return _list;
            }
        }
        ...
        public Myclass (List<X> input)
        {
          _list = new List <X>(input);
        }
        ...
     }

class Testclass
    {
      List<X> inputList = new List<X>() {"bla", "bla"};
      Myclass myclass = new Myclass(inputlist);

      myclass.list.Clear();
      List<X> newL = myclass.list;
    }

I'm not supposed to change anything with the second class "Testclass".

And this is my problem: myclass.list.Clear() clears the public list AND the private list. But I only want it to clear the public list or do nothing at all because in the end I need the list newL to be filled with the items out of the private list (public list via get-accessor).

Since I'm supposed to solve the problem without touching the second class I have no idea what to do. I've been told that the public list is called a property, is this right? If so, what is the public list called?

Like I said, I'm pretty bad/new in programming in general (19 y/o) so beginner-friendly answers would be much appreciated!


Solution

  • public List<X> list
    {
        get
        {
            return new List<X>(_list);
        }
    }
    

    This will return a copy of the list every time. So from the outside, the caller never gets access to the real list on the inside. He can do with the copy whatever he wants, clear, add, remove. It will not change the private list.

    Please note that in real production code you would instead change the property to not return a list, but something that simply does not have the option to change it (like an IEnumerable<T>). Giving somebody a List and then ignoring whatever he does with it is very unintuitive for the caller and not exactly good programming.