
从我的理解,因为虚空有它自己的堆栈它是线程安全的,但是我不完全确定在使用输出时是否为真.有人可以澄清这个问题.谢谢!
namespace ThreadTest{class Program{ static voID Main(string[] args) { int helloWorldint = 0; bool helloWorldbool = false; int helloWorldintOut = 0; bool helloWorldboolOut = false; setHelloWorlds(helloWorldint,helloWorldbool,out helloWorldintOut,out helloWorldboolOut); Console.Writeline(helloWorldintOut); Console.Writeline(helloWorldboolOut); } public static voID setHelloWorlds(int helloWorldint,bool helloWorldbool,out int helloWorldintOut,out bool helloWorldboolOut) { helloWorldintOut = helloWorldint + 1; helloWorldboolOut = true; }}}解决方法 从MSDN文档: The out keyword causes arguments to be passed by reference. This is like the ref keyword,except that ref requires that the variable be initialized before it is passed. To use an out parameter,both the method deFinition and the calling method must explicitly use the out keyword.
所以问题的答案取决于你如何调用静态方法.由于变量是通过引用传递的,如果你有多个线程调用你的方法并且它们传入相同的变量引用作为参数(即使这些参数是值类型,因为OUT导致通过引用显式传递),那么你的方法不是线程安全的.另一方面,如果多个线程调用您的方法,每个线程都传入自己的变量引用,那么您的方法将是线程安全的.
这不是特定于OUT或REF修饰符.修改引用类型数据的任何方法本身并不是线程安全的,您应该仔细考虑选择该路由的原因.通常,对于线程安全的方法,它应该是非常好的封装.
总结以上是内存溢出为你收集整理的c# – 带参数的静态方法是否安全?全部内容,希望文章能够帮你解决c# – 带参数的静态方法是否安全?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)