List and other classes are reference types. In few words, it means you have an object somewhere in memory and a reference(s) on it.
this.l = l;
means you copied the reference to the first list to the class field. So you have one list and two references on it. And when you clear the list via a
variable, no matter how you address it after clearing - via a
or cl.l
. Your single list is cleared already.
If you want to avoid this, you need to create a copy of list in your constructor:
public cl(List<char> l)
{
this.l = new List<char>();
this.l.AddRange(l);
}
}
I recommend you to read more information about reference types. They are used widely and knowledge about them will give you a good base for programming skills.