简体   繁体   中英

Is static method thread safe

If I have a static method to convert one object to another object, is this method thread safe in C#?

public static AnotherDataClass Convert(MyDataClass target)
{
     AnotherDataClass val = new AnotherDataClass();
     // read infomration from target
     // put information into val;
     return val;
}

Just want to make the question more clear....

when invoke the convert method.... we can assume that target is not going be be modified. since the Convert method only interested in the "attrubite" of target

No, it is not.

"A method would be thread safe if it would b accessing data that won't be accessible to any other thread" If this definition is correct then the method is not thread safe

Reason

MyDataClass seems reference type to me so there is a chance that multiple threads might be changing the target variable

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM