Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Real-Time Segments for Feature Experimentation segment qualification methods for the C# SDK

Use the FetchQualifiedSegments method to retrieve the external audience mapping for the user from the Optimizely Data Platform (ODP) server. Use the IsQualifiedFor method to check if the user qualifies for the specified segment.


You must enable the Real-Time Segments for Feature Experimentation integration before fetching qualified segments and checking if the user is qualified for the given audience segment.


Minimum SDK Version



You can use the FetchQualifiedSegments method to retrieve the external audience mapping for a specific user from the Optimizely Data Platform (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.

FetchQualfiedSegments is a method of the UserContext object. See OptimizelyUserContext for details.


The following table describes the parameters for the fetchQualifiedSegments method:

callback (for asynchronous calls)Callback functionA completion handler to be called with the fetch result.
segmentOptions (optional)ListA 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.

Returns – Asynchronous call

  • If the fetch completes successfully, the C# SDK updates the qualified segments array in the OptimizelyUserContext 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.



You can read and write directly to the qualified segments list instead of calling FetchQualifiedSegments() using the GetQualifiedSegments() and SetQualifiedSegments().

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

Example FetchQualifiedSegments call

The code creates a user context by instantiating an OptimizelyUserContext object with a user ID of "user123" 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 theFetchQualifiedSegments method to fetch all qualified segments with and without segment options.

After that, the code uses the Decide 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 method to track a custom event called "myevent".

var optimizely = OptimizelyFactory.NewDefaultInstance();
var attributes = new UserAttributes
    { "app_version", "1.3.2" },
var userContext = optimizely.CreateUserContext("user123", attributes);

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

// Do other work here


// Do more work that depends on `success`
if (success)
var optimizely = OptimizelyFactory.NewDefaultInstance();
var userContext = optimizely.CreateUserContext("user123", new UserAttributes
    { "app_version", "1.3.2" },
var success = userContext.FetchQualifiedSegments();
if (success)

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

  1. Call FetchQualifiedSegments method.
  2. The C# SDK makes GraphQL call to ODP to fetch segments.
  3. ODP responds with the qualified segments.
  4. Fetched segments mapping user IDs to segments are cached, see more below.
  5. Appropriate variations are returned for user.

The SDK then caches the fetched segments. This means that if the same user requests 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 segments 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 the 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.


Minimum SDK version



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


The following table describes the parameters for the IsQualifiedFor method:

segmentstringThe ODP audience segment name to check if the user is qualified for.


true if the user is qualified.


The following is an example of whether or not the user is qualified for an ODP segment:

var projectConfigurationManager = new HttpProjectConfigManager.Builder()
    .Build(defer: true);
var optimizely = new Optimizely(projectConfigurationManager);
var attributes = new UserAttributes
    { "laptop_os", "mac" },
var userContext = optimizely.CreateUserContext("user123", attributes);

Source files

The language and platform source files containing the implementation for C# are available on GitHub