C
C#17mo ago
imnota4

❔ Can separate class objects contain instances of the same method object

So if I have multiple objects of the same class, with a particular member object that is in itself a class, can those separate class objects all refer to the same object when referring to their own member object, similar to how a pointer would work?
4 Replies
Jimmacle
Jimmacle17mo ago
the way you're phrasing it is confusing, but generally yes you can think of reference types as working similar to pointers
var obj1 = new object();
var obj2 = obj1;
var obj1 = new object();
var obj2 = obj1;
will make obj1 and obj2 refer to the same actual object
Angius
Angius17mo ago
yes, reference types work kinda-sorta like pointers So
var foo = new Foo();
var barA = new Bar { Foo = foo };
var barB = new Bar { Foo = foo };
var foo = new Foo();
var barA = new Bar { Foo = foo };
var barB = new Bar { Foo = foo };
will have both barA and barB have the exact same reference to foo
Omnissiah
Omnissiah17mo ago
(since we're talking about pointers, i think it could be worth mentioning ref var obj3 = ref obj1;)
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server