Can you reference (without passing the object) an object created in your main from another class if given the same name and in the same namespace in C#?
Like say you have Main, Class A and Class B. In Main you have “A ap = new A();”
If you create “A ap = new A();” inside of Class B, would both of those object reference the same instance of the object A? So that if you changed a value in object “ap” on your main, it would also change that same variable for object “ap” in class B. Basically do they point to the same instance of Class A in the Heap?
Every time you use
new, you are creating a new object on the heap, as the name implies.Now if you want the two classes to point to the same object, you could pass the object
ap(or more specifically, the value of the reference variableap) into class B and they would point to the same object.