---
title: "Get Compare Exchange Value Operation"
url: "https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value"
version: "6.2"
language: "csharp"
---

# Get Compare Exchange Value Operation

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value.java.md), [Node.js](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-value.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - Use `GetCompareExchangeValueOperation` to return the saved compare-exchange *Value* for the specified *Key*.
>
> - For an overview of the 'Compare Exchange' feature click: [Compare Exchange Overview](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/overview.md)
>
> - In this page:
>   - [Syntax](#syntax)
>   - [Example I - Value is 'long'](#example-i---value-is-)
>   - [Example II - Value is a custom object](#example-ii---value-is-a-custom-object)

## Syntax

**Method**:

```csharp
public GetCompareExchangeValueOperation(string key)
```

**Returned object**:

```csharp
public class CompareExchangeValue<T>
{
  public readonly string Key;
  public readonly T Value;
  public readonly long Index;
}
```

| Parameter | Type | Description |
| - | - | - |
| **Key** | string | The unique object identifier |
| **Value** | `T` | The existing value that *Key* has |
| **Index** | long | The version number of the *Value* that is stored for the specified *Key* |

> **Info:** Session Interface and Lazy Get {#session-interface-and-lazy-get}
>
> You can also get compare exchange values through the [session cluster transactions](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/compare-exchange.md#get-compare-exchange)
> at `session.Advanced.ClusterTransaction`.
>
> This method also exposes methods getting compare exchange [lazily](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/compare-exchange.md#get-compare-exchange).

## Example I - Value is 'long'

```csharp
CompareExchangeValue<long> readResult =
  store.Operations.Send(new GetCompareExchangeValueOperation<long>("NextClientId"));

long value = readResult.Value;
```

## Example II - Value is a custom object

```csharp
CompareExchangeValue<User> readResult =
  store.Operations.Send(new GetCompareExchangeValueOperation<User>("AdminUser"));

User admin = readResult.Value;
```

## See also

- [Overview](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/overview.md)
- [Get Compare-Exchange Values](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/get-compare-exchange-values.md)
- [Put a Compare-Exchange Value](https://docs.ravendb.net/6.2/client-api/operations/compare-exchange/delete-compare-exchange-value.md)
- [Cluster Transaction - Overview](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/overview.md)
- [Operations overview](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.md)
