METHOD
Interlocked.Exchange
Atomically replaces a supported location and returns its previous value. Does not coordinate several independent locations or perform resource cleanup automatically.
Formal Syntax & Structure
csharp syntaxcsharp
Interlocked.Exchange(ref location, newValue)Working Example
Example Snippetcsharp
using System;using System.IO;using System.Collections.Generic;using System.Buffers;using System.Buffers.Binary;using System.Runtime.CompilerServices;using System.Threading;int n=3; Console.WriteLine(Interlocked.Exchange(ref n,7)); Console.WriteLine(n); // 3, 7