开发者

Arguments to delegate.BeginInvoke are copies or references?

开发者 https://www.devze.com 2022-12-27 13:12 出处:网络
Where is there documentation on the treatment of arguments to BeginInvoke? If I have a delegate (which wraps my handler function) that accepts an object as a parameter, does that object get copied or

Where is there documentation on the treatment of arguments to BeginInvoke?

If I have a delegate (which wraps my handler function) that accepts an object as a parameter, does that object get copied or referenced by the asynchronously-called handler function?

delegate void MyDelegate(SomeObject obj);

// later o开发者_Python百科n:
// invoke the delegate async'ly:
new MyDelegate(StaticClass.HandlerFunc).BeginInvoke(objInstance, null, null);
// alter the object:
objInstance.SomeProperty = newValue;

// function:
public static void HandlerFunc(SomeObject obj) {
   // is it a possible race condition to read SomeProperty:
   if(obj.SomeProperty == oldValue) {
      // will possibly never enter?
   }
   // ... etc.
}


The method gets a reference to the object.

Objects aren't copied in .NET, unless you specifically create a copy.

0

精彩评论

暂无评论...
验证码 换一张
取 消