Interlocked.CompareExchange 方法

定义

比较两个值是否相等,如果相等,则替换第一个值作为原子操作。

重载

CompareExchange(UIntPtr, UIntPtr, UIntPtr)

比较两个特定于平台的句柄或指针是否相等,如果它们相等,则替换第一个句柄作为原子操作。

CompareExchange(UInt64, UInt64, UInt64)

比较两个 64 位无符号整数是否相等,如果相等,请将第一个值替换为原子运算。

CompareExchange(UInt32, UInt32, UInt32)

比较两个 32 位无符号整数是否相等,如果相等,则替换第一个值作为原子运算。

CompareExchange(UInt16, UInt16, UInt16)

比较两个 16 位有符号整数是否相等,如果相等,则替换第一个值作为原子运算。

CompareExchange(Single, Single, Single)

比较两个单精度浮点数是否相等,如果相等,则替换第一个值作为原子运算。

CompareExchange(Object, Object, Object)

比较两个对象的引用相等性,如果两个对象相等,则替换第一个对象作为原子操作。

CompareExchange(SByte, SByte, SByte)

比较两个 8 位有符号整数是否相等,如果相等,则替换第一个值作为原子操作。

CompareExchange(Int64, Int64, Int64)

比较两个 64 位有符号整数是否相等,如果相等,则替换第一个值作为原子运算。

CompareExchange(Int32, Int32, Int32)

比较两个 32 位有符号整数是否相等,如果相等,则替换第一个值作为原子运算。

CompareExchange(Int16, Int16, Int16)

比较两个 16 位无符号整数是否相等,如果相等,则替换第一个值作为原子运算。

CompareExchange(Double, Double, Double)

比较两个双精度浮点数是否相等,如果相等,则替换第一个值作为原子运算。

CompareExchange(Byte, Byte, Byte)

比较两个 8 位无符号整数是否相等,如果相等,则替换第一个值作为原子操作。

CompareExchange(IntPtr, IntPtr, IntPtr)

比较两个特定于平台的句柄或指针是否相等,如果它们相等,则替换第一个句柄作为原子操作。

CompareExchange<T>(T, T, T)

比较指定引用类型的两个实例,T 引用相等,如果相等,则替换第一个实例作为原子操作。

CompareExchange(UIntPtr, UIntPtr, UIntPtr)

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

重要

此 API 不符合 CLS。

比较两个特定于平台的句柄或指针是否相等,如果它们相等,则替换第一个句柄作为原子操作。

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

参数

location1
UIntPtr

目标 UIntPtr,其值与 comparand 值进行比较,并可能替换为 value

value
UIntPtr

如果比较结果相等,则替换目标值的 UIntPtr

comparand
UIntPtr

location1的值进行比较的 UIntPtr

返回

UIntPtr

location1中的原始值。

属性

例外

location1 的地址是 null 指针。

适用于

.NET 10 和其他版本
产品 版本
.NET 7, 8, 9, 10

CompareExchange(UInt64, UInt64, UInt64)

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

重要

此 API 不符合 CLS。

比较两个 64 位无符号整数是否相等,如果相等,请将第一个值替换为原子运算。

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

参数

location1
UInt64

目标,其值与 comparand 进行比较,并可能替换。

value
UInt64

如果比较结果相等,则替换目标值的值。

comparand
UInt64

location1的值进行比较的值。

返回

location1中的原始值。

属性

例外

location1 的地址是 null 指针。

适用于

.NET 10 和其他版本
产品 版本
.NET 5, 6, 7, 8, 9, 10

CompareExchange(UInt32, UInt32, UInt32)

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

重要

此 API 不符合 CLS。

比较两个 32 位无符号整数是否相等,如果相等,则替换第一个值作为原子运算。

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

参数

location1
UInt32

目标,其值与 comparand 进行比较,并可能替换。

value
UInt32

如果比较结果相等,则替换目标值的值。

comparand
UInt32

location1的值进行比较的值。

返回

location1中的原始值。

属性

例外

location1 的地址是 null 指针。

适用于

.NET 10 和其他版本
产品 版本
.NET 5, 6, 7, 8, 9, 10

CompareExchange(UInt16, UInt16, UInt16)

重要

此 API 不符合 CLS。

比较两个 16 位有符号整数是否相等,如果相等,则替换第一个值作为原子运算。

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

参数

location1
UInt16

目标,其值与 comparand 进行比较,并可能替换。

value
UInt16

如果比较结果相等,则替换目标值的值。

comparand
UInt16

location1的值进行比较的值。

返回

location1中的原始值。

属性

例外

location1 的地址是 null 指针。

适用于

.NET 10 和 .NET 9
产品 版本
.NET 9, 10

CompareExchange(Single, Single, Single)

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

比较两个单精度浮点数是否相等,如果相等,则替换第一个值作为原子运算。

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

参数

location1
Single

目标,其值与 comparand 进行比较,并可能替换。

value
Single

如果比较结果相等,则替换目标值的值。

comparand
Single

location1的值进行比较的值。

返回

location1中的原始值。

例外

location1 的地址为 null 指针。

示例

下面的代码示例演示了一种线程安全方法,该方法累积了总 Single 值。 两个线程使用线程安全方法和普通加法添加一系列 Single 值,并在线程完成总计时进行比较。 在双处理器计算机上,总计存在显著差异。

在线程安全方法中,保存运行总计的初始值,然后使用 CompareExchange 方法将新计算的总和与旧总计交换。 如果返回值不等于正在运行总计的已保存值,则另一个线程同时更新了总计。 在这种情况下,必须重复尝试更新正在运行的总和。

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
 */

注解

如果 comparandlocation1 中的值相等,则 value 存储在 location1中。 否则,不执行任何操作。 比较和交换操作作为原子操作执行。 CompareExchange 的返回值是 location1中的原始值,无论交换是否发生。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.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, 10
.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

比较两个对象的引用相等性,如果两个对象相等,则替换第一个对象作为原子操作。

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

参数

location1
Object

通过引用与 comparand 比较并可能替换的目标对象。

value
Object

如果引用比较结果相等,则替换目标对象的对象。

comparand
Object

通过引用 location1对象进行比较的对象。

返回

location1中的原始值。

例外

location1 的地址是 null 指针。

注解

重要

从 .NET Framework 2.0 开始,CompareExchange<T>(T, T, T) 方法重载为引用类型提供类型安全的替代方法。 建议调用它,而不是此重载。

如果 comparandlocation1 中的对象按引用相等,则 value 存储在 location1中。 否则,不执行任何操作。 比较和交换操作作为原子操作执行。 CompareExchange 的返回值是 location1中的原始值,无论交换是否发生。

备注

将对象与引用相等性而不是值相等性进行比较。 因此,具有相同值类型的两个装箱实例(例如整数 3)始终不相等,并且不执行任何操作。 不要将此重载用于值类型。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.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, 10
.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)

重要

此 API 不符合 CLS。

比较两个 8 位有符号整数是否相等,如果相等,则替换第一个值作为原子操作。

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

参数

location1
SByte

目标,其值与 comparand 进行比较,并可能替换。

value
SByte

如果比较结果相等,则替换目标值的值。

comparand
SByte

location1的值进行比较的值。

返回

location1中的原始值。

属性

例外

location1 的地址是 null 指针。

适用于

.NET 10 和 .NET 9
产品 版本
.NET 9, 10

CompareExchange(Int64, Int64, Int64)

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

比较两个 64 位有符号整数是否相等,如果相等,则替换第一个值作为原子运算。

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

参数

location1
Int64

目标,其值与 comparand 进行比较,并可能替换。

value
Int64

如果比较结果相等,则替换目标值的值。

comparand
Int64

location1的值进行比较的值。

返回

location1中的原始值。

例外

location1 的地址为 null 指针。

注解

如果 comparandlocation1 中的值相等,则 value 存储在 location1中。 否则,不执行任何操作。 比较和交换操作作为原子操作执行。 CompareExchange 的返回值是 location1中的原始值,无论交换是否发生。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.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, 10
.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

比较两个 32 位有符号整数是否相等,如果相等,则替换第一个值作为原子运算。

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

参数

location1
Int32

目标,其值与 comparand 进行比较,并可能替换。

value
Int32

如果比较结果相等,则替换目标值的值。

comparand
Int32

location1的值进行比较的值。

返回

location1中的原始值。

例外

location1 的地址为 null 指针。

示例

下面的代码示例演示了一种线程安全方法,该方法累积了总运行总数。 保存运行总计的初始值,然后使用 CompareExchange 方法将新计算的总和与旧总计交换。 如果返回值不等于正在运行总计的已保存值,则另一个线程同时更新了总计。 在这种情况下,必须重复尝试更新正在运行的总和。

备注

.NET Framework 版本 2.0 中引入的 Add 方法为整数累积线程安全的运行总计提供了更方便的方法。

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;
    }
}

注解

如果 comparandlocation1 中的值相等,则 value 存储在 location1中。 否则,不执行任何操作。 比较和交换操作作为原子操作执行。 CompareExchange 的返回值是 location1中的原始值,无论交换是否发生。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.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, 10
.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)

比较两个 16 位无符号整数是否相等,如果相等,则替换第一个值作为原子运算。

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

参数

location1
Int16

目标,其值与 comparand 进行比较,并可能替换。

value
Int16

如果比较结果相等,则替换目标值的值。

comparand
Int16

location1的值进行比较的值。

返回

location1中的原始值。

例外

location1 的地址是 null 指针。

适用于

.NET 10 和 .NET 9
产品 版本
.NET 9, 10

CompareExchange(Double, Double, Double)

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

比较两个双精度浮点数是否相等,如果相等,则替换第一个值作为原子运算。

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

参数

location1
Double

目标,其值与 comparand 进行比较,并可能替换。

value
Double

如果比较结果相等,则替换目标值的值。

comparand
Double

location1的值进行比较的值。

返回

location1中的原始值。

例外

location1 的地址为 null 指针。

示例

下面的代码示例演示了一种线程安全方法,该方法累积了总 Double 值。 两个线程使用线程安全方法和普通加法添加一系列 Double 值,并在线程完成总计时进行比较。 在双处理器计算机上,总计存在显著差异。

在线程安全方法中,保存运行总计的初始值,然后使用 CompareExchange 方法将新计算的总和与旧总计交换。 如果返回值不等于正在运行总计的已保存值,则另一个线程同时更新了总计。 在这种情况下,必须重复尝试更新正在运行的总和。

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
 */

注解

如果 comparandlocation1 中的值相等,则 value 存储在 location1中。 否则,不执行任何操作。 比较和交换操作作为原子操作执行。 CompareExchange 的返回值是 location1中的原始值,无论交换是否发生。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.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, 10
.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)

比较两个 8 位无符号整数是否相等,如果相等,则替换第一个值作为原子操作。

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

参数

location1
Byte

目标,其值与 comparand 进行比较,并可能替换。

value
Byte

如果比较结果相等,则替换目标值的值。

comparand
Byte

location1的值进行比较的值。

返回

location1中的原始值。

例外

location1 的地址是 null 指针。

适用于

.NET 10 和 .NET 9
产品 版本
.NET 9, 10

CompareExchange(IntPtr, IntPtr, IntPtr)

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

比较两个特定于平台的句柄或指针是否相等,如果它们相等,则替换第一个句柄作为原子操作。

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

参数

location1
IntPtr

目标 IntPtr,其值与 comparand 值进行比较,并可能替换为 value

value
IntPtr

如果比较结果相等,则替换目标值的 IntPtr

comparand
IntPtr

location1的值进行比较的 IntPtr

返回

IntPtr

location1中的原始值。

例外

location1 的地址为 null 指针。

注解

如果 comparandlocation1 中的值相等,则 value 存储在 location1中。 否则,不执行任何操作。 比较和交换操作作为原子操作执行。 此方法的返回值是 location1中的原始值,无论交换是否发生。

备注

IntPtr 是特定于平台的类型。

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.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, 10
.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

比较指定引用类型的两个实例,T 引用相等,如果相等,则替换第一个实例作为原子操作。

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;

类型参数

T

要用于 location1valuecomparand的类型。 此类型必须是引用类型。

参数

location1
T

目标,其值通过引用与 comparand 进行比较,并可能被替换。 这是一个引用参数(在 C# 中ref,在 Visual Basic 中 ByRef)。

value
T

如果引用的比较结果相等,则替换目标值的值。

comparand
T

通过引用 location1的值进行比较的值。

返回

T

location1中的原始值。

属性

例外

location1 的地址为 null 指针。

指定了不受支持的 T

注解

如果 comparandlocation1 中的值按引用相等,则 value 存储在 location1中。 否则,不执行任何操作。 比较和交换作为原子操作执行。 此方法的返回值是 location1中的原始值,无论交换是否发生。

此方法仅支持引用类型。 值类型有 CompareExchange 重载 Int32Int64IntPtrSingleDouble,但不支持其他值类型。

备注

此方法重载优于 CompareExchange(Object, Object, Object) 方法重载,因为后者要求后期访问目标对象。

适用于

.NET 10 和其他版本
产品 版本
.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, 10
.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