2017-10-11 49 views
0

我熟悉使用Interlocked.CompareExchange()與普通對象。不過,我想使用它與數組的成員:如何與Interlocked.CompareExchange一起使用數組項目

string[] myArray = new string[] { "A", "B", "C" }; 
string myStr = (string) Interlocked.CompareExchange(ref myArray[0], null, myArray[0]); 
// myArray[0] == null 

我該如何做到這一點?

+1

你對這個解決方案有什麼問題? – Servy

+0

好吧,一切看起來都不錯。運行你的代碼並且非常好。我不確定我是否看到你需要幫助的地方。 – Iman

+0

我真的誤解了我原來的錯誤! –

回答

1

我使用它像這樣

這是出把

F B C 

一切都很好。

1

我沒看到問題。您將數組中的第一個字符串設置爲null。這就是爲什麼它是空的。這是你如何使用它:

string[] myArray = new string[] { "A", "B", "C" }; 
string myStr = (string) Interlocked.CompareExchange(ref myArray[0], ASDF /* VALUE */, 
myArray[0]); 
//myArray[0] == "ASDF" <- VALUE YOU SET 
相關問題