C# thread safety for class instances
- by Steveng
I am learning C# and I am confused with the thread safety of the copies of the class instances as below:
eg:
classA objA;
classA objB = objA;
objA.field1 = value2; //do I need lock around modification of field1?
//let say we pass the objB to another thread
objB.field1 = value1 //do I need a lock for objB because of the modification of field1?
I am confused because coming from the background of C++, the class in C# is the reference type. If both objA and objB refer to the same memory underlying, then I would need a lock to protect the simultaneous writing to the field1. Could someone confirm with this or am I missing something?
Thanks.