Is static method thread safe

南笙酒味 提交于 2019-12-10 16:15:27

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/8370636/is-static-method-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!