Interlocked.Exchange Method

Definition

Sets a variable to a specified value as an atomic operation.

Overloads

Exchange(UInt16, UInt16)

Sets a 16-bit signed integer to a specified value and returns the original value, as an atomic operation.

Exchange(UInt32, UInt32)

Sets a 32-bit unsigned integer to a specified value and returns the original value, as an atomic operation.

Exchange(Single, Single)

Sets a single-precision floating point number to a specified value and returns the original value, as an atomic operation.

Exchange(UIntPtr, UIntPtr)

Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.

Exchange(UInt64, UInt64)

Sets a 64-bit unsigned integer to a specified value and returns the original value, as an atomic operation.

Exchange(SByte, SByte)

Sets a 8-bit signed integer to a specified value and returns the original value, as an atomic operation.

Exchange(Byte, Byte)

Sets a 8-bit unsigned integer to a specified value and returns the original value, as an atomic operation.

Exchange(IntPtr, IntPtr)

Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.

Exchange(Int64, Int64)

Sets a 64-bit signed integer to a specified value and returns the original value, as an atomic operation.

Exchange(Int32, Int32)

Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation.

Exchange(Int16, Int16)

Sets a 16-bit unsigned integer to a specified value and returns the original value, as an atomic operation.

Exchange(Double, Double)

Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation.

Exchange(Object, Object)

Sets an object to a specified value and returns a reference to the original object, as an atomic operation.

Exchange<T>(T, T)

Sets a variable of the specified type T to a specified value and returns the original value, as an atomic operation.

Exchange(UInt16, UInt16)

Important

This API is not CLS-compliant.

Sets a 16-bit signed integer to a specified value and returns the original value, as an atomic operation.

[System.CLSCompliant(false)]
public static ushort Exchange (ref ushort location1, ushort value);

Parameters

location1
UInt16

The variable to set to the specified value.

value
UInt16

The value to which the location1 parameter is set.

Returns

The original value of location1.

Attributes

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9
Product Versions
.NET 9

Exchange(UInt32, UInt32)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

Important

This API is not CLS-compliant.

Sets a 32-bit unsigned integer to a specified value and returns the original value, as an atomic operation.

[System.CLSCompliant(false)]
public static uint Exchange (ref uint location1, uint value);

Parameters

location1
UInt32

The variable to set to the specified value.

value
UInt32

The value to which the location1 parameter is set.

Returns

The original value of location1.

Attributes

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9 and other versions
Product Versions
.NET 5, 6, 7, 8, 9

Exchange(Single, Single)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

Sets a single-precision floating point number to a specified value and returns the original value, as an atomic operation.

public static float Exchange (ref float location1, float value);

Parameters

location1
Single

The variable to set to the specified value.

value
Single

The value to which the location1 parameter is set.

Returns

The original value of location1.

Exceptions

The address of location1 is a null pointer.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Exchange(UIntPtr, UIntPtr)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

Important

This API is not CLS-compliant.

Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.

[System.CLSCompliant(false)]
public static UIntPtr Exchange (ref UIntPtr location1, UIntPtr value);

Parameters

location1
UIntPtr

The variable to set to the specified value.

value
UIntPtr

The value to which the location1 parameter is set.

Returns

UIntPtr

The original value of location1.

Attributes

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

Exchange(UInt64, UInt64)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

Important

This API is not CLS-compliant.

Sets a 64-bit unsigned integer to a specified value and returns the original value, as an atomic operation.

[System.CLSCompliant(false)]
public static ulong Exchange (ref ulong location1, ulong value);

Parameters

location1
UInt64

The variable to set to the specified value.

value
UInt64

The value to which the location1 parameter is set.

Returns

The original value of location1.

Attributes

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9 and other versions
Product Versions
.NET 5, 6, 7, 8, 9

Exchange(SByte, SByte)

Important

This API is not CLS-compliant.

Sets a 8-bit signed integer to a specified value and returns the original value, as an atomic operation.

[System.CLSCompliant(false)]
public static sbyte Exchange (ref sbyte location1, sbyte value);

Parameters

location1
SByte

The variable to set to the specified value.

value
SByte

The value to which the location1 parameter is set.

Returns

The original value of location1.

Attributes

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9
Product Versions
.NET 9

Exchange(Byte, Byte)

Sets a 8-bit unsigned integer to a specified value and returns the original value, as an atomic operation.

public static byte Exchange (ref byte location1, byte value);

Parameters

location1
Byte

The variable to set to the specified value.

value
Byte

The value to which the location1 parameter is set.

Returns

The original value of location1.

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9
Product Versions
.NET 9

Exchange(IntPtr, IntPtr)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.

public static IntPtr Exchange (ref IntPtr location1, IntPtr value);

Parameters

location1
IntPtr

The variable to set to the specified value.

value
IntPtr

The value to which the location1 parameter is set.

Returns

IntPtr

The original value of location1.

Exceptions

The address of location1 is a null pointer.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Exchange(Int64, Int64)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.CoreCLR.cs

Sets a 64-bit signed integer to a specified value and returns the original value, as an atomic operation.

public static long Exchange (ref long location1, long value);

Parameters

location1
Int64

The variable to set to the specified value.

value
Int64

The value to which the location1 parameter is set.

Returns

The original value of location1.

Exceptions

The address of location1 is a null pointer.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Exchange(Int32, Int32)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.CoreCLR.cs

Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation.

public static int Exchange (ref int location1, int value);

Parameters

location1
Int32

The variable to set to the specified value.

value
Int32

The value to which the location1 parameter is set.

Returns

The original value of location1.

Exceptions

The address of location1 is a null pointer.

The address of location1 is a null pointer.

Examples

The following code example shows a thread-safe resource locking mechanism.

using System;
using System.Threading;

namespace InterlockedExchange_Example
{
    class MyInterlockedExchangeExampleClass
    {
        //0 for false, 1 for true.
        private static int usingResource = 0;

        private const int numThreadIterations = 5;
        private const int numThreads = 10;

        static void Main()
        {
            Thread myThread;
            Random rnd = new Random();

            for(int i = 0; i < numThreads; i++)
            {
                myThread = new Thread(new ThreadStart(MyThreadProc));
                myThread.Name = String.Format("Thread{0}", i + 1);
            
                //Wait a random amount of time before starting next thread.
                Thread.Sleep(rnd.Next(0, 1000));
                myThread.Start();
            }
        }

        private static void MyThreadProc()
        {
            for(int i = 0; i < numThreadIterations; i++)
            {
                UseResource();
            
                //Wait 1 second before next attempt.
                Thread.Sleep(1000);
            }
        }

        //A simple method that denies reentrancy.
        static bool UseResource()
        {
            //0 indicates that the method is not in use.
            if(0 == Interlocked.Exchange(ref usingResource, 1))
            {
                Console.WriteLine("{0} acquired the lock", Thread.CurrentThread.Name);
            
                //Code to access a resource that is not thread safe would go here.
            
                //Simulate some work
                Thread.Sleep(500);

                Console.WriteLine("{0} exiting lock", Thread.CurrentThread.Name);
            
                //Release the lock
                Interlocked.Exchange(ref usingResource, 0);
                return true;
            }
            else
            {
                Console.WriteLine("   {0} was denied the lock", Thread.CurrentThread.Name);
                return false;
            }
        }
    }
}

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Exchange(Int16, Int16)

Sets a 16-bit unsigned integer to a specified value and returns the original value, as an atomic operation.

public static short Exchange (ref short location1, short value);

Parameters

location1
Int16

The variable to set to the specified value.

value
Int16

The value to which the location1 parameter is set.

Returns

The original value of location1.

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9
Product Versions
.NET 9

Exchange(Double, Double)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation.

public static double Exchange (ref double location1, double value);

Parameters

location1
Double

The variable to set to the specified value.

value
Double

The value to which the location1 parameter is set.

Returns

The original value of location1.

Exceptions

The address of location1 is a null pointer.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Exchange(Object, Object)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.CoreCLR.cs

Sets an object to a specified value and returns a reference to the original object, as an atomic operation.

public static object Exchange (ref object location1, object value);
public static object? Exchange (ref object? location1, object? value);

Parameters

location1
Object

The variable to set to the specified value.

value
Object

The value to which the location1 parameter is set.

Returns

The original value of location1.

Exceptions

The address of location1 is a null pointer.

The address of location1 is a null pointer.

Remarks

Important

Beginning with .NET Framework 2.0, the Exchange<T>(T, T) method overload provides a type-safe alternative for reference types. We recommend that you call it instead of this overload.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Exchange<T>(T, T)

Source:
Interlocked.CoreCLR.cs
Source:
Interlocked.CoreCLR.cs
Source:
Interlocked.CoreCLR.cs

Sets a variable of the specified type T to a specified value and returns the original value, as an atomic operation.

public static T Exchange<T> (ref T location1, T value) where T : class;
public static T Exchange<T> (ref T location1, T value);
[System.Runtime.InteropServices.ComVisible(false)]
public static T Exchange<T> (ref T location1, T value) where T : class;

Type Parameters

T

The type to be used for location1 and value. This type must be a reference type.

Parameters

location1
T

The variable to set to the specified value. This is a reference parameter (ref in C#, ByRef in Visual Basic).

value
T

The value to which the location1 parameter is set.

Returns

T

The original value of location1.

Attributes

Exceptions

The address of location1 is a null pointer.

An unsupported T is specified.

Remarks

This method only supports reference types. There are overloads of the Exchange method for the Int32, Int64, IntPtr, Single, and Double value types, but there is no support for other value types.

Note

This method overload is preferable to the Exchange(Object, Object) method overload, because the latter requires late-bound access to the destination object .

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0