Interlocked.Exchange<T> Method (T, T)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Sets a variable of the specified type T to a specified value and returns the original value, as an atomic operation.

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

public static T Exchange<T>(
	ref T location1,
	T value
where T : class


Type: T

The variable to set to the specified value. This is a reference parameter (ref in C#, ByRef in Visual Basic).

Type: T

The value to which the location1 parameter is set.

Return Value

Type: T

The original value of location1.

Type Parameters


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

Exception Condition

The address of location1 is a null pointer.

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


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

The following example launches ten tasks, each of which generates 100 ten-character part numbers. It then the part number that is first alphabetically.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public class Widget
   public Widget(String id)
      this.Id = id;

   public String Id { get; set; }
   public String Description { get; set; }

public class Example
   public static void Main()
      Widget firstWidget = null;
      var tasks = new List<Task>();
      for (int ctr = 0; ctr <= 10; ctr++) {
         tasks.Add(Task.Run( () => { // Give each task its own random number generator.
                                     var rnd = new Random();
                                     for (int widgetIndex = 0; widgetIndex <= 100; widgetIndex++) {
                                        //Generate ten random characters from U+0041 to U+005A.
                                        String id = String.Empty;
                                        for (int charCtr = 0; charCtr <= 9; charCtr++)
                                           id += Convert.ToChar(rnd.Next(0x0041, 0x005B));

                                        var newWidget = new Widget(id);
                                        if (firstWidget == null)
                                           firstWidget = newWidget;
                                        else if (newWidget.Id.CompareTo(firstWidget.Id) < 0)
                                           Interlocked.Exchange(ref firstWidget, newWidget);

                                   } ));
      try {
         Console.WriteLine("The widget with the lowest id: {0}", firstWidget.Id);
      catch (AggregateException ae) {
         foreach (var e in ae.InnerExceptions)
            Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
// The example displays output like the following:
//   The widget with the lowest id: ACHZVFBYNU

The example uses the firstWidget variable to store the ID that occurs first in the sort order. It compares each ID that it generates with the firstWidget value. If the new value precedes the current value of firstWidget in the sort order, the example calls the Exchange<T> method to assign the new value to firstWidget.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top