Interlocked.CompareExchange Method

Definition

Compares two values for equality and, if they are equal, replaces the first value, as an atomic operation.

Overloads

CompareExchange(UIntPtr, UIntPtr, UIntPtr)

Compares two platform-specific handles or pointers for equality and, if they are equal, replaces the first one, as an atomic operation.

CompareExchange(UInt64, UInt64, UInt64)

Compares two 64-bit unsigned integers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(UInt32, UInt32, UInt32)

Compares two 32-bit unsigned integers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(UInt16, UInt16, UInt16)

Compares two 16-bit signed integers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(Single, Single, Single)

Compares two single-precision floating point numbers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(Object, Object, Object)

Compares two objects for reference equality and, if they are equal, replaces the first object, as an atomic operation.

CompareExchange(SByte, SByte, SByte)

Compares two 8-bit signed integers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(Int64, Int64, Int64)

Compares two 64-bit signed integers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(Int32, Int32, Int32)

Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(Int16, Int16, Int16)

Compares two 16-bit unsigned integers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(Double, Double, Double)

Compares two double-precision floating point numbers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(Byte, Byte, Byte)

Compares two 8-bit unsigned integers for equality and, if they are equal, replaces the first value, as an atomic operation.

CompareExchange(IntPtr, IntPtr, IntPtr)

Compares two platform-specific handles or pointers for equality and, if they are equal, replaces the first one, as an atomic operation.

CompareExchange<T>(T, T, T)

Compares two instances of the specified reference type T for reference equality and, if they are equal, replaces the first one, as an atomic operation.

CompareExchange(UIntPtr, UIntPtr, UIntPtr)

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

Important

This API is not CLS-compliant.

Compares two platform-specific handles or pointers for equality and, if they are equal, replaces the first one, as an atomic operation.

C#
[System.CLSCompliant(false)]
public static UIntPtr CompareExchange(ref UIntPtr location1, UIntPtr value, UIntPtr comparand);

Parameters

location1
UIntPtr

The destination UIntPtr, whose value is compared with the value of comparand and possibly replaced by value.

value
UIntPtr

The UIntPtr that replaces the destination value if the comparison results in equality.

comparand
UIntPtr

The UIntPtr that is compared to the value at location1.

Returns

UIntPtr

The original value in location1.

Attributes

Exceptions

The address of location1 is a null pointer.

Applies to

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

CompareExchange(UInt64, UInt64, UInt64)

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

Important

This API is not CLS-compliant.

Compares two 64-bit unsigned integers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
[System.CLSCompliant(false)]
public static ulong CompareExchange(ref ulong location1, ulong value, ulong comparand);

Parameters

location1
UInt64

The destination, whose value is compared with comparand and possibly replaced.

value
UInt64

The value that replaces the destination value if the comparison results in equality.

comparand
UInt64

The value that is compared to the value at location1.

Returns

The original value in 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

CompareExchange(UInt32, UInt32, UInt32)

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

Important

This API is not CLS-compliant.

Compares two 32-bit unsigned integers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
[System.CLSCompliant(false)]
public static uint CompareExchange(ref uint location1, uint value, uint comparand);

Parameters

location1
UInt32

The destination, whose value is compared with comparand and possibly replaced.

value
UInt32

The value that replaces the destination value if the comparison results in equality.

comparand
UInt32

The value that is compared to the value at location1.

Returns

The original value in 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

CompareExchange(UInt16, UInt16, UInt16)

Important

This API is not CLS-compliant.

Compares two 16-bit signed integers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
[System.CLSCompliant(false)]
public static ushort CompareExchange(ref ushort location1, ushort value, ushort comparand);

Parameters

location1
UInt16

The destination, whose value is compared with comparand and possibly replaced.

value
UInt16

The value that replaces the destination value if the comparison results in equality.

comparand
UInt16

The value that is compared to the value at location1.

Returns

The original value in location1.

Attributes

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9
Product Versions
.NET 9

CompareExchange(Single, Single, Single)

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

Compares two single-precision floating point numbers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
public static float CompareExchange(ref float location1, float value, float comparand);

Parameters

location1
Single

The destination, whose value is compared with comparand and possibly replaced.

value
Single

The value that replaces the destination value if the comparison results in equality.

comparand
Single

The value that is compared to the value at location1.

Returns

The original value in location1.

Exceptions

The address of location1 is a null pointer.

Examples

The following code example demonstrates a thread-safe method that accumulates a running total of Single values. Two threads add a series of Single values using the thread-safe method and ordinary addition, and when the threads complete the totals are compared. On a dual-processor computer, there is a significant difference in the totals.

In the thread-safe method, the initial value of the running total is saved, and then the CompareExchange method is used to exchange the newly computed total with the old total. If the return value is not equal to the saved value of the running total, then another thread has updated the total in the meantime. In that case, the attempt to update the running total must be repeated.

C#
// This example demonstrates a thread-safe method that adds to a
// running total.  
using System;
using System.Threading;

public class ThreadSafe
{
    // Field totalValue contains a running total that can be updated
    // by multiple threads. It must be protected from unsynchronized 
    // access.
    private float totalValue = 0.0F;

    // The Total property returns the running total.
    public float Total { get { return totalValue; }}

    // AddToTotal safely adds a value to the running total.
    public float AddToTotal(float addend)
    {
        float initialValue, computedValue;
        do
        {
            // Save the current running total in a local variable.
            initialValue = totalValue;

            // Add the new value to the running total.
            computedValue = initialValue + addend;

            // CompareExchange compares totalValue to initialValue. If
            // they are not equal, then another thread has updated the
            // running total since this loop started. CompareExchange
            // does not update totalValue. CompareExchange returns the
            // contents of totalValue, which do not equal initialValue,
            // so the loop executes again.
        }
        while (initialValue != Interlocked.CompareExchange(ref totalValue, 
            computedValue, initialValue));
        // If no other thread updated the running total, then 
        // totalValue and initialValue are equal when CompareExchange
        // compares them, and computedValue is stored in totalValue.
        // CompareExchange returns the value that was in totalValue
        // before the update, which is equal to initialValue, so the 
        // loop ends.

        // The function returns computedValue, not totalValue, because
        // totalValue could be changed by another thread between
        // the time the loop ends and the function returns.
        return computedValue;
    }
}

public class Test
{
    // Create an instance of the ThreadSafe class to test.
    private static ThreadSafe ts = new ThreadSafe();
    private static float control;

    private static Random r = new Random();
    private static ManualResetEvent mre = new ManualResetEvent(false);

    public static void Main()
    {
        // Create two threads, name them, and start them. The
        // thread will block on mre.
        Thread t1 = new Thread(TestThread);
        t1.Name = "Thread 1";
        t1.Start();
        Thread t2 = new Thread(TestThread);
        t2.Name = "Thread 2";
        t2.Start();

        // Now let the threads begin adding random numbers to 
        // the total.
        mre.Set();
        
        // Wait until all the threads are done.
        t1.Join();
        t2.Join();

        Console.WriteLine("Thread safe: {0}  Ordinary float: {1}", 
            ts.Total, control);
    }

    private static void TestThread()
    {
        // Wait until the signal.
        mre.WaitOne();

        for(int i = 1; i <= 1000000; i++)
        {
            // Add to the running total in the ThreadSafe instance, and
            // to an ordinary float.
            //
            float testValue = (float) r.NextDouble();
            control += testValue;
            ts.AddToTotal(testValue);
        }
    }
}

/* On a dual-processor computer, this code example produces output 
   similar to the following:

Thread safe: 17039.57  Ordinary float: 15706.44
 */

Remarks

If comparand and the value in location1 are equal, then value is stored in location1. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

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

CompareExchange(Object, Object, Object)

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

Compares two objects for reference equality and, if they are equal, replaces the first object, as an atomic operation.

C#
public static object CompareExchange(ref object location1, object value, object comparand);
C#
public static object? CompareExchange(ref object? location1, object? value, object? comparand);

Parameters

location1
Object

The destination object that is compared by reference with comparand and possibly replaced.

value
Object

The object that replaces the destination object if the reference comparison results in equality.

comparand
Object

The object that is compared by reference to the object at location1.

Returns

The original value in location1.

Exceptions

The address of location1 is a null pointer.

Remarks

Important

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

If comparand and the object in location1 are equal by reference, then value is stored in location1. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

Note

The objects are compared for reference equality rather than value equality. As a result, two boxed instances of the same value type (for example, the integer 3) always appear to be unequal and no operation is performed. Do not use this overload with value types.

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

CompareExchange(SByte, SByte, SByte)

Important

This API is not CLS-compliant.

Compares two 8-bit signed integers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
[System.CLSCompliant(false)]
public static sbyte CompareExchange(ref sbyte location1, sbyte value, sbyte comparand);

Parameters

location1
SByte

The destination, whose value is compared with comparand and possibly replaced.

value
SByte

The value that replaces the destination value if the comparison results in equality.

comparand
SByte

The value that is compared to the value at location1.

Returns

The original value in location1.

Attributes

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9
Product Versions
.NET 9

CompareExchange(Int64, Int64, Int64)

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

Compares two 64-bit signed integers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
public static long CompareExchange(ref long location1, long value, long comparand);

Parameters

location1
Int64

The destination, whose value is compared with comparand and possibly replaced.

value
Int64

The value that replaces the destination value if the comparison results in equality.

comparand
Int64

The value that is compared to the value at location1.

Returns

The original value in location1.

Exceptions

The address of location1 is a null pointer.

Remarks

If comparand and the value in location1 are equal, then value is stored in location1. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

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

CompareExchange(Int32, Int32, Int32)

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

Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
public static int CompareExchange(ref int location1, int value, int comparand);

Parameters

location1
Int32

The destination, whose value is compared with comparand and possibly replaced.

value
Int32

The value that replaces the destination value if the comparison results in equality.

comparand
Int32

The value that is compared to the value at location1.

Returns

The original value in location1.

Exceptions

The address of location1 is a null pointer.

Examples

The following code example demonstrates a thread-safe method that accumulates a running total. The initial value of the running total is saved, and then the CompareExchange method is used to exchange the newly computed total with the old total. If the return value is not equal to the saved value of the running total, then another thread has updated the total in the meantime. In that case, the attempt to update the running total must be repeated.

Note

The Add method, introduced in version 2.0 of the .NET Framework, provides a more convenient way to accumulate thread-safe running totals for integers.

C#
// This example demonstrates a thread-safe method that adds to a
// running total.  It cannot be run directly.  You can compile it
// as a library, or add the class to a project.
using System.Threading;

public class ThreadSafe {
    // totalValue contains a running total that can be updated
    // by multiple threads. It must be protected from unsynchronized 
    // access.
    private int totalValue = 0;

    // The Total property returns the running total.
    public int Total {
        get { return totalValue; }
    }

    // AddToTotal safely adds a value to the running total.
    public int AddToTotal(int addend) {
        int initialValue, computedValue;
        do {
            // Save the current running total in a local variable.
            initialValue = totalValue;

            // Add the new value to the running total.
            computedValue = initialValue + addend;

            // CompareExchange compares totalValue to initialValue. If
            // they are not equal, then another thread has updated the
            // running total since this loop started. CompareExchange
            // does not update totalValue. CompareExchange returns the
            // contents of totalValue, which do not equal initialValue,
            // so the loop executes again.
        } while (initialValue != Interlocked.CompareExchange(
            ref totalValue, computedValue, initialValue));
        // If no other thread updated the running total, then 
        // totalValue and initialValue are equal when CompareExchange
        // compares them, and computedValue is stored in totalValue.
        // CompareExchange returns the value that was in totalValue
        // before the update, which is equal to initialValue, so the 
        // loop ends.

        // The function returns computedValue, not totalValue, because
        // totalValue could be changed by another thread between
        // the time the loop ends and the function returns.
        return computedValue;
    }
}

Remarks

If comparand and the value in location1 are equal, then value is stored in location1. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

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

CompareExchange(Int16, Int16, Int16)

Compares two 16-bit unsigned integers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
public static short CompareExchange(ref short location1, short value, short comparand);

Parameters

location1
Int16

The destination, whose value is compared with comparand and possibly replaced.

value
Int16

The value that replaces the destination value if the comparison results in equality.

comparand
Int16

The value that is compared to the value at location1.

Returns

The original value in location1.

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9
Product Versions
.NET 9

CompareExchange(Double, Double, Double)

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

Compares two double-precision floating point numbers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
public static double CompareExchange(ref double location1, double value, double comparand);

Parameters

location1
Double

The destination, whose value is compared with comparand and possibly replaced.

value
Double

The value that replaces the destination value if the comparison results in equality.

comparand
Double

The value that is compared to the value at location1.

Returns

The original value in location1.

Exceptions

The address of location1 is a null pointer.

Examples

The following code example demonstrates a thread-safe method that accumulates a running total of Double values. Two threads add a series of Double values using the thread-safe method and ordinary addition, and when the threads complete the totals are compared. On a dual-processor computer, there is a significant difference in the totals.

In the thread-safe method, the initial value of the running total is saved, and then the CompareExchange method is used to exchange the newly computed total with the old total. If the return value is not equal to the saved value of the running total, then another thread has updated the total in the meantime. In that case, the attempt to update the running total must be repeated.

C#
// This example demonstrates a thread-safe method that adds to a
// running total.  
using System;
using System.Threading;

public class ThreadSafe
{
    // Field totalValue contains a running total that can be updated
    // by multiple threads. It must be protected from unsynchronized 
    // access.
    private double totalValue = 0.0;

    // The Total property returns the running total.
    public double Total { get { return totalValue; }}

    // AddToTotal safely adds a value to the running total.
    public double AddToTotal(double addend)
    {
        double initialValue, computedValue;
        do
        {
            // Save the current running total in a local variable.
            initialValue = totalValue;

            // Add the new value to the running total.
            computedValue = initialValue + addend;

            // CompareExchange compares totalValue to initialValue. If
            // they are not equal, then another thread has updated the
            // running total since this loop started. CompareExchange
            // does not update totalValue. CompareExchange returns the
            // contents of totalValue, which do not equal initialValue,
            // so the loop executes again.
        }
        while (initialValue != Interlocked.CompareExchange(ref totalValue, 
            computedValue, initialValue));
        // If no other thread updated the running total, then 
        // totalValue and initialValue are equal when CompareExchange
        // compares them, and computedValue is stored in totalValue.
        // CompareExchange returns the value that was in totalValue
        // before the update, which is equal to initialValue, so the 
        // loop ends.

        // The function returns computedValue, not totalValue, because
        // totalValue could be changed by another thread between
        // the time the loop ends and the function returns.
        return computedValue;
    }
}

public class Test
{
    // Create an instance of the ThreadSafe class to test.
    private static ThreadSafe ts = new ThreadSafe();
    private static double control;

    private static Random r = new Random();
    private static ManualResetEvent mre = new ManualResetEvent(false);

    public static void Main()
    {
        // Create two threads, name them, and start them. The
        // thread will block on mre.
        Thread t1 = new Thread(TestThread);
        t1.Name = "Thread 1";
        t1.Start();
        Thread t2 = new Thread(TestThread);
        t2.Name = "Thread 2";
        t2.Start();

        // Now let the threads begin adding random numbers to 
        // the total.
        mre.Set();
        
        // Wait until all the threads are done.
        t1.Join();
        t2.Join();

        Console.WriteLine("Thread safe: {0}  Ordinary Double: {1}", 
            ts.Total, control);
    }

    private static void TestThread()
    {
        // Wait until the signal.
        mre.WaitOne();

        for(int i = 1; i <= 1000000; i++)
        {
            // Add to the running total in the ThreadSafe instance, and
            // to an ordinary double.
            //
            double testValue = r.NextDouble();
            control += testValue;
            ts.AddToTotal(testValue);
        }
    }
}

/* On a dual-processor computer, this code example produces output 
   similar to the following:

Thread safe: 998068.049623744  Ordinary Double: 759775.417190589
 */

Remarks

If comparand and the value in location1 are equal, then value is stored in location1. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

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

CompareExchange(Byte, Byte, Byte)

Compares two 8-bit unsigned integers for equality and, if they are equal, replaces the first value, as an atomic operation.

C#
public static byte CompareExchange(ref byte location1, byte value, byte comparand);

Parameters

location1
Byte

The destination, whose value is compared with comparand and possibly replaced.

value
Byte

The value that replaces the destination value if the comparison results in equality.

comparand
Byte

The value that is compared to the value at location1.

Returns

The original value in location1.

Exceptions

The address of location1 is a null pointer.

Applies to

.NET 9
Product Versions
.NET 9

CompareExchange(IntPtr, IntPtr, IntPtr)

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

Compares two platform-specific handles or pointers for equality and, if they are equal, replaces the first one, as an atomic operation.

C#
public static IntPtr CompareExchange(ref IntPtr location1, IntPtr value, IntPtr comparand);

Parameters

location1
IntPtr

The destination IntPtr, whose value is compared with the value of comparand and possibly replaced by value.

value
IntPtr

The IntPtr that replaces the destination value if the comparison results in equality.

comparand
IntPtr

The IntPtr that is compared to the value at location1.

Returns

IntPtr

The original value in location1.

Exceptions

The address of location1 is a null pointer.

Remarks

If comparand and the value in location1 are equal, then value is stored in location1. Otherwise, no operation is performed. The compare and exchange operations are performed as an atomic operation. The return value of this method is the original value in location1, whether or not the exchange takes place.

Note

IntPtr is a platform-specific type.

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

CompareExchange<T>(T, T, T)

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

Compares two instances of the specified reference type T for reference equality and, if they are equal, replaces the first one, as an atomic operation.

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

Type Parameters

T

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

Parameters

location1
T

The destination, whose value is compared by reference with comparand and possibly replaced. This is a reference parameter (ref in C#, ByRef in Visual Basic).

value
T

The value that replaces the destination value if the comparison by reference results in equality.

comparand
T

The value that is compared by reference to the value at location1.

Returns

T

The original value in location1.

Attributes

Exceptions

The address of location1 is a null pointer.

An unsupported T is specified.

Remarks

If comparand and the value in location1 are equal by reference, then value is stored in location1. Otherwise, no operation is performed. The comparison and the exchange are performed as an atomic operation. The return value of this method is the original value in location1, whether or not the exchange takes place.

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

Note

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

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