Interlocked.Add 方法

定义

以原子操作的形式,添加两个整数并用两者的和替换第一个整数。Adds two integers and replaces the first integer with the sum, as an atomic operation.

重载

Add(Int32, Int32)

对两个 32 位整数进行求和并用和替换第一个整数,上述操作作为一个原子操作完成。Adds two 32-bit integers and replaces the first integer with the sum, as an atomic operation.

Add(Int64, Int64)

对两个 64 位整数进行求和并用和替换第一个整数,上述操作作为一个原子操作完成。Adds two 64-bit integers and replaces the first integer with the sum, as an atomic operation.

Add(UInt32, UInt32)

对两个 32 位无符号整数进行求和并用和替换第一个整数,上述操作作为一个原子操作完成。Adds two 32-bit unsigned integers and replaces the first integer with the sum, as an atomic operation.

Add(UInt64, UInt64)

对两个 64 位无符号整数进行求和并用和替换第一个整数,上述操作作为一个原子操作完成。Adds two 64-bit unsigned integers and replaces the first integer with the sum, as an atomic operation.

Add(Int32, Int32)

对两个 32 位整数进行求和并用和替换第一个整数,上述操作作为一个原子操作完成。Adds two 32-bit integers and replaces the first integer with the sum, as an atomic operation.

public:
 static int Add(int % location1, int value);
public static int Add (ref int location1, int value);
static member Add : int * int -> int
Public Shared Function Add (ByRef location1 As Integer, value As Integer) As Integer

参数

location1
Int32

一个变量,包含要添加的第一个值。A variable containing the first value to be added. 两个值的和存储在 location1 中。The sum of the two values is stored in location1.

value
Int32

要添加到整数中的 location1 位置的值。The value to be added to the integer at location1.

返回

Int32

存储在 location1 处的新值。The new value stored at location1.

例外

location1 的地址为空指针。The address of location1 is a null pointer.

注解

此方法通过包装处理溢出条件:如果中的值 location1 为, Int32.MaxValue 并且 value 为1,则结果为 Int32.MinValue ; 如果 value 为2,则结果为 (Int32.MinValue + 1) ; 依此类推。This method handles an overflow condition by wrapping: if the value at location1 is Int32.MaxValue and value is 1, the result is Int32.MinValue; if value is 2, the result is (Int32.MinValue + 1); and so on. 不会引发异常。No exception is thrown.

适用于

Add(Int64, Int64)

对两个 64 位整数进行求和并用和替换第一个整数,上述操作作为一个原子操作完成。Adds two 64-bit integers and replaces the first integer with the sum, as an atomic operation.

public:
 static long Add(long % location1, long value);
public static long Add (ref long location1, long value);
static member Add : int64 * int64 -> int64
Public Shared Function Add (ByRef location1 As Long, value As Long) As Long

参数

location1
Int64

一个变量,包含要添加的第一个值。A variable containing the first value to be added. 两个值的和存储在 location1 中。The sum of the two values is stored in location1.

value
Int64

要添加到整数中的 location1 位置的值。The value to be added to the integer at location1.

返回

Int64

存储在 location1 处的新值。The new value stored at location1.

例外

location1 的地址为空指针。The address of location1 is a null pointer.

注解

此方法通过包装处理溢出条件:如果中的值 location1 为, Int64.MaxValue 并且 value 为1,则结果为 Int64.MinValue ; 如果 value 为2,则结果为 (Int64.MinValue + 1) ; 依此类推。This method handles an overflow condition by wrapping: if the value at location1 is Int64.MaxValue and value is 1, the result is Int64.MinValue; if value is 2, the result is (Int64.MinValue + 1); and so on. 不会引发异常。No exception is thrown.

另请参阅

适用于

Add(UInt32, UInt32)

重要

此 API 不符合 CLS。

对两个 32 位无符号整数进行求和并用和替换第一个整数,上述操作作为一个原子操作完成。Adds two 32-bit unsigned integers and replaces the first integer with the sum, as an atomic operation.

public:
 static System::UInt32 Add(System::UInt32 % location1, System::UInt32 value);
[System.CLSCompliant(false)]
public static uint Add (ref uint location1, uint value);
public static uint Add (ref uint location1, uint value);
[<System.CLSCompliant(false)>]
static member Add : uint32 * uint32 -> uint32
static member Add : uint32 * uint32 -> uint32
Public Shared Function Add (ByRef location1 As UInteger, value As UInteger) As UInteger

参数

location1
UInt32

一个变量,包含要添加的第一个值。A variable containing the first value to be added. 两个值的和存储在 location1 中。The sum of the two values is stored in location1.

value
UInt32

要添加到整数中的 location1 位置的值。The value to be added to the integer at location1.

返回

UInt32

存储在 location1 处的新值。The new value stored at location1.

属性

例外

location1 的地址是一个 null 指针。The address of location1 is a null pointer.

适用于

Add(UInt64, UInt64)

重要

此 API 不符合 CLS。

对两个 64 位无符号整数进行求和并用和替换第一个整数,上述操作作为一个原子操作完成。Adds two 64-bit unsigned integers and replaces the first integer with the sum, as an atomic operation.

public:
 static System::UInt64 Add(System::UInt64 % location1, System::UInt64 value);
[System.CLSCompliant(false)]
public static ulong Add (ref ulong location1, ulong value);
public static ulong Add (ref ulong location1, ulong value);
[<System.CLSCompliant(false)>]
static member Add : uint64 * uint64 -> uint64
static member Add : uint64 * uint64 -> uint64
Public Shared Function Add (ByRef location1 As ULong, value As ULong) As ULong

参数

location1
UInt64

一个变量,包含要添加的第一个值。A variable containing the first value to be added. 两个值的和存储在 location1 中。The sum of the two values is stored in location1.

value
UInt64

要添加到整数中的 location1 位置的值。The value to be added to the integer at location1.

返回

UInt64

存储在 location1 处的新值。The new value stored at location1.

属性

例外

location1 的地址是一个 null 指针。The address of location1 is a null pointer.

适用于