This documentation is archived and is not being maintained.

Interlocked.Decrement Method (Int32%)

Decrements a specified variable and stores the result, as an atomic operation.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public static int Decrement(
	ref int location


Type: System.Int32%
The variable whose value is to be decremented.

Return Value

Type: System.Int32
The decremented value.


The address of location is a null pointer.

This method handles an overflow condition by wrapping: If location = Int32.MinValue, location - 1 = Int32.MaxValue. No exception is thrown.

The following code example shows a thread-safe way to increment and decrement an integer value. SafeInstanceCount will always be zero. However, UnsafeInstanceCount will not necessarily be zero because a race condition occurs between incrementing and decrementing the count. This effect is especially marked on a multiprocessor computer.


The C++ example does not follow the IDisposable.Dispose pattern. (See Destructors and Finalizers in Visual C++.) The finalizer is used to decrement the counters, so that garbage collection causes decrementing.

using System;
using System.Threading;

class Test
    static void Main()
        Thread thread1 = new Thread(new ThreadStart(ThreadMethod));
        Thread thread2 = new Thread(new ThreadStart(ThreadMethod));

        // Have the garbage collector run the finalizer for each
        // instance of CountClass and wait for it to finish.

        Console.WriteLine("UnsafeInstanceCount: {0}" +
            "\nSafeCountInstances: {1}",

    static void ThreadMethod()
        CountClass cClass;

        // Create 100,000 instances of CountClass.
        for(int i = 0; i < 100000; i++)
            cClass = new CountClass();

class CountClass
    static int unsafeInstanceCount = 0;
    static int   safeInstanceCount = 0;

    static public int UnsafeInstanceCount
        get {return unsafeInstanceCount;}

    static public int SafeInstanceCount
        get {return safeInstanceCount;}

    public CountClass()
        Interlocked.Increment(ref safeInstanceCount);

        Interlocked.Decrement(ref safeInstanceCount);

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.