I have a class A that contains a List field.
public class A { public List<int> list = new List<int>(); } I would like to remove an element from the list from class B without making the list in class A static. How can I do that?
You could create an instance of class A inside a method in class B. Then you could access the list field, like this:
public class B { void method() { A a = new A(); int item = 2; a.list.Remove(item); } } If you don't mind instantiating it, it's simply
A a = new A(); a.list... If you don't want to instantiate a new one, you can pass an existing instance to B on its constructor:
public class B{ private A myA; public B( A a) { this.myA = a; } public doSomething(){ this.myA.... } } Now you can use A as a field of B.
publicso anyone with an instance of classAcan already get tolistand modify it.