---
title: Real-Time Audiences for Feature Experimentation segment qualification methods for the C# SDK
slug: feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-csharp
docTags: 
createdAt: 2026-08-05T20:39:59.000Z
---

## Prerequisites

You must enable the [Real-Time Audiences for Feature Experimentation integration](docId:6UBBxxRv331nBIVZ2lun8) before fetching qualified segments and checking if the user is qualified for the given audience segment.

## FetchQualifiedSegments

### Minimum SDK Version

4.0.0

### Description

You can use the `FetchQualifiedSegments` method to retrieve the external audience mapping for a specific user from the ODP server. The Optimizely Feature Experimentation C# SDK provides a synchronous and asynchronous version of the `FetchQualifiedSegments` method.

- The SDK blocks the caller until after completing the synchronous fetch.
- The SDK does not block the caller of asynchronous API.

`FetchQualifiedSegments` is a method of the `UserContext` object. See [OptimizelyUserContext](docId\:UZTTnJ2LeXwXXWe_aZvjr) for details.

### Parameters

The following table describes the parameters for the `fetchQualifiedSegments` method:

| Parameter                           | Type                    | Description                                                |
| ----------------------------------- | ----------------------- | ---------------------------------------------------------- |
| `callback` (for asynchronous calls) | Callback function       | A completion handler to be called with the fetch result.   |
| `segmentOptions` (optional)         | List\<OdpSegmentOption> | A set of options for fetching qualified segments from ODP. |

### Returns  –   Synchronous call

The `FetchQualifiedSegments` synchronous method returns `true` if the qualified segments array is in the  [OptimizelyUserContext](docId\:UZTTnJ2LeXwXXWe_aZvjr).

### Returns  –   Asynchronous call

- If the fetch completes, the C# SDK updates the qualified segments array in the  [OptimizelyUserContext](docId\:UZTTnJ2LeXwXXWe_aZvjr) and then invokes the callback handler with a success status.
- If the fetch fails, the SDK calls the handler with a failure status.

The asynchronous `FetchQualifiedSegments` returns a `Task` object, which can be used to `.Wait()` for the task to complete.

If the C# SDK does not find an ODP audience in the datafile, it returns an empty qualified segments array without sending a request to the ODP server.

:::hint{type="info"}
**Note**
You can read and write directly to the qualified segments array instead of calling `FetchQualifiedSegments()` using the `GetQualifiedSegments()` and `SetQualifiedSegments()`.

This lets you bypass the remote fetching process from ODP or use your own fetching service. This can be helpful when testing or debugging.
:::

### Example `FetchQualifiedSegments` call

The following code sample creates a user context by instantiating an `OptimizelyUserContext` object with a user ID of "fs-id-123" and a set of attributes that includes an "app\_version" attribute with a value of "1.3.2".

The next section of the code shows the use of the`FetchQualifiedSegments` method to fetch all qualified segments with and without segment options.

After that, the code uses the [\`Decide\`](docId\:QB-OuF0F-Tj3XpdYgXX1L) method to make a decision on whether to show the feature flag with the key "flag1" to the user. Finally, the code calls the [\`TrackEvent\`](docId\:t2cdt_4CYXEkRKj2R_Qx9) method to track a custom event called "myevent".

```csharp
var optimizely = OptimizelyFactory.NewDefaultInstance();
var attributes = new UserAttributes
{
    { "app_version", "1.3.2" },
};
var userContext = optimizely.CreateUserContext("fs-id-123", attributes);

var success = false;
var task = userContext.FetchQualifiedSegments(result => success = result);

// Do other work here

task.Wait();

// Do more work that depends on `success`
if (success)
{
    userContext.Decide("flag1");
    userContext.TrackEvent("myevent");
}
```

```csharp
var optimizely = OptimizelyFactory.NewDefaultInstance();
var userContext = optimizely.CreateUserContext("fs-id-123", new UserAttributes
{
    { "app_version", "1.3.2" },
});
var success = userContext.FetchQualifiedSegments();
if (success)
{
    userContext.Decide("flag1");
    userContext.TrackEvent("myevent");
}
```

The following diagram shows the network calls between your application, the C# SDK, and the ODP server when calling `FetchQualifiedSegments`:

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/seTBDGVS8hlD5O8aZqDvS_5968e007edd66e6e7670308c51bfc975efe9da23350d68fc4a3e6d97d40c0931-rts-network-diagrams-c-fetchqualifiedsegments.png)

1. Your application calls the `FetchQualifiedSegments` method.
2. The Feature Experimentation C# SDK makes a GraphQL call to ODP to fetch segments the user ID qualifies for.
3. ODP responds with the qualified audience segments.
4. The C# SDK automatically caches the fetched segments mapping the user ID to the qualified segments.
5. The `FetchQualifiedSegments` method returns a boolean if the qualified segments array was updated.
6. Call the `Decide` method to get the flag decision for a given user, a decision object that contains the flag's status (enabled or disabled), and the assigned variation key.

The C# SDK automatically caches the fetched segments. This means that if you request the segments again (when new user contexts are created), you can retrieve the audience segment information from the cache rather than fetch it from the remote ODP server.

The cache is used for the `FetchQualifiedSegments` call. This method is called on the user context (the user context is fixed, including the real-time audiences the user qualifies for).

The cache *only* applies when calling the `FetchQualifiedSegments` call. If you set the cache timeout to 0, the cache is disabled. Optimizely uses a least recently used (LRU) algorithm, so the oldest record is bumped out when the maximum size is reached. If there is a cache miss upon the method call, Optimizely makes a network request.

You can bypass caching by adding the following options to your `OdpSegmentOptions` array:

- `IGNORE_CACHE` – Bypass segments cache for lookup and save.
- `RESET_CACHE` – Reset all segments cache.

## IsQualifiedFor

### Minimum SDK version

v4.0.0

### Description

Check if the user is qualified for the given audience segment.

### Parameters

The following table describes the parameters for the `IsQualifiedFor` method:

| Parameter | Type   | Description                                                          |
| --------- | ------ | -------------------------------------------------------------------- |
| segment   | string | The ODP audience segment name to check if the user is qualified for. |

### Returns

`true` if the user is qualified.

### Examples

The following is a code sample showing whether the user is qualified for an ODP segment:

```csharp
var projectConfigurationManager = new HttpProjectConfigManager.Builder()
    .WithSdkKey("YOUR_SDK_KEY")
    .WithPollingInterval(TimeSpan.FromSeconds(5))
    .WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(500))
    .Build(defer: true);
var optimizely = new Optimizely(projectConfigurationManager);
var attributes = new UserAttributes
{
    { "laptop_os", "mac" },
};
var userContext = optimizely.CreateUserContext("user123", attributes);
userContext.FetchQualifiedSegments();
userContext.IsQualifiedFor("segment1");
```

## Source files

The language and platform source files containing the implementation for C# are available on [GitHub](https://github.com/optimizely/csharp-sdk/tree/master/OptimizelySDK/Odp)
