For instance:
void SomeMethod()
{
MyObject o = new MyObject();
// Do stuff with o
SomeAsyncMethod(o);
o = new MyObject(); // Will this affect what was passed to SomeAsyncMethod?
}
Anything I do to 'o' will be obviously be apparent in both the main and the new thread. However, if I set o equal to a new instance in the main thread it shouldn't change the fact the the variable in SomeAsyncMethod() is still pointing to the original instance of the object right?
No, it will have no effect on other o. however
void SomeMethod()
{
MyObject o = new MyObject();
// Do stuff with o
SomeAsyncMethod(o);
o.Id = 2222; // will change objects Id property, which will
// be reflected in another thread
}
the reason is in your code you are changing value (reference) of local variable o, not the object itself
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.