# How to increment by 5 using Interlocked.Increment

• ### Question

• i saw Interlocked.Increment. does it always increment by 1 ? what to change as a result Interlocked.Increment does the increment by 5 or 10 ?
Wednesday, January 16, 2019 6:04 PM

• Hi,

yes it increments by 1. Maybe you want to use Add instead.

```public static int Increment(ref int location);

public static int Add(ref int location1, int value);```

Example:

`i = Interlocked.Add(ref j, 5);`

Here is the documentation of the method:

• Edited by Wednesday, January 16, 2019 8:38 PM
• Marked as answer by Thursday, January 17, 2019 3:36 PM
Wednesday, January 16, 2019 8:33 PM
• Hi Sudip_inn,

Thank you for posting here.

For your question, the Interlocked.Increment Method do something like number++ in the code. This method is the same as Add, but they use the value of 1 as an implicit argument. It could not be used to increase by 5.

As DerChris88 said, you could use Add method to instead to increase with 5. Here is a simple example for your reference.

Run the A method twice, it will return 10.

```class Program
{
static int _value;
static void Main(string[] args)
{
Console.WriteLine(_value);
}

static void A()
{
}
}```

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Marked as answer by Thursday, January 17, 2019 3:36 PM
Thursday, January 17, 2019 7:07 AM

### All replies

• Hi,

yes it increments by 1. Maybe you want to use Add instead.

```public static int Increment(ref int location);

public static int Add(ref int location1, int value);```

Example:

`i = Interlocked.Add(ref j, 5);`

Here is the documentation of the method:

• Edited by Wednesday, January 16, 2019 8:38 PM
• Marked as answer by Thursday, January 17, 2019 3:36 PM
Wednesday, January 16, 2019 8:33 PM
• Hi Sudip_inn,

Thank you for posting here.

For your question, the Interlocked.Increment Method do something like number++ in the code. This method is the same as Add, but they use the value of 1 as an implicit argument. It could not be used to increase by 5.

As DerChris88 said, you could use Add method to instead to increase with 5. Here is a simple example for your reference.

Run the A method twice, it will return 10.

```class Program
{
static int _value;
static void Main(string[] args)
{
Console.WriteLine(_value);
}

static void A()
{