---
title: OptimizelyUserContext for the C# SDK
slug: feature-experimentation/docs/optimizelyusercontext-csharp
docTags: 
createdAt: 2026-08-05T20:40:02.000Z
---

The `OptimizelyUserContext` object lets you make flag decisions and track events for a user context that you have already created using the [Create User Context](docId\:NMLEiJmrOW1XL9kGEplxb) method.

## OptimizelyUserContext minimum SDK version

SDK v3.7 and higher.

### Forced decision methods minimum SDK version

`SetForcedDecision()`, `GetForcedDecision()`, `RemoveForcedDecision()` and `RemoveAllForcedDecisions()` methods are supported on v3.11.0 and higher.

### Real-Time Audiences for Feature Experimentation minimum SDK version

`FetchQualifiedSegments()` and `IsQualifiedFor()` methods are supported on version 4.0.0 and higher.

## OptimizelyUserContext updates

```csharp
public class OptimizelyUserContext
{
	public OptimizelyUserContext(Optimizely optimizely,
		string userId,
		UserAttributes userAttributes,
		ForcedDecisionsStore forcedDecisionsStore,
		IErrorHandler errorHandler, ILogger logger);
  
    // Set an attribute for the user
    public void SetAttribute(string key, object value);

    // Get attributes for the user
    public UserAttributes GetAttributes();

    // Make a decision about which flag variation the user buckets into for the flag key 
    public OptimizelyDecision Decide(string key, OptimizelyDecideOption[] options);

    // Make decisions about which flag variations the user buckets into for flag keys
    public Dictionary<string, OptimizelyDecision> DecideForKeys(string[] keys,
	    OptimizelyDecideOption[] options);

    // Make decisions about which flag variations the user buckets into for all flags
    public Dictionary<string, OptimizelyDecision> DecideAll(OptimizelyDecideOption[] options);

    // Track user event
    public void TrackEvent(string eventName, EventTags eventTags);

    // Sets a forced decision (variationKey) for a given decision context
    public bool SetForcedDecision(OptimizelyDecisionContext context,
	    OptimizelyForcedDecision decision);

    // Returns the forced decision (variationKey) for a given decision context
    public OptimizelyForcedDecision GetForcedDecision(OptimizelyDecisionContext context);

    // Removes the forced decision (variationKey) for a given decision context
    public bool RemoveForcedDecision(OptimizelyDecisionContext context);

    // Removes all the forced decisions (variationKeys) for the user context
    public bool RemoveAllForcedDecisions();
    
    /*
     * The following methods require Real-Time Audiences for Feature Experimentation.
     * See the note following the code sample.
     */

    // An array of segment names that the user is qualified for. 
    // The result of **fetchQualifiedSegments()** is saved here. 
    // Can be null if not properly updated with fetchQualifiedSegments(). 
    // 
    // You can read and write directly to the qualified segments array. 
    // This lets you bypass the remote fetching process from ODP 
    // or for utilizing your own fetching service.   
    public List<string> GetQualifiedSegments();
    
    // Set the qualified audiences that the user is qualified for.
    public void SetQualifiedSegments(List<string> qualifiedSegments);

    // Fetch all qualified segments for the user context. 
    // The audiences fetched are saved in the **QualifiedSegments** array
    // and can be accessed any time. 
    public bool FetchQualifiedSegments(List<OdpSegmentOption> segmentOptions = null);

    // Fetch all qualified audiences for the user context in a non-blocking manner. 
    // This method fetches segments in a separate thread and invokes the provided 
    // callback when results are available.
    public Task FetchQualifiedSegments(Action<bool> callback,
	    List<OdpSegmentOption> segmentOptions = null);
    
    //  Check if the user is qualified for the given segment. 
    public bool IsQualifiedFor(string segment);
}
```

:::hint{type="info"}
**Note**
You must first enable and configure Real-Time Audiences for Feature Experimentation to use the `GetQualifiedSegments()`, `SetQualifiedSegments()`, `FetchQualifiedSegments()`, and `IsQualifiedFor()` methods. See [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8).
:::

## Properties

| Attribute             | Type       | Comment                                                                                                                                                                                                                                      |
| --------------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| userId                | string     | The ID of the user                                                                                                                                                                                                                           |
| (optional) attributes | Dictionary | A dictionary of custom key-value pairs specifying attributes for the user used for \[audience targeting]\(https\://support.optimizely.com/hc/en-us/articles/38683417490957). You can pass the map with the user ID when you create the user. |

## Methods

The following table shows methods for the `OptimizelyUserContext` object:

| Method                      | Comment                                                                                                                                                                                                                                                                                                                                                                                    |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| SetAttribute                | Pass a custom user attribute as a key-value pair to the user context.                                                                                                                                                                                                                                                                                                                      |
| GetAttributes               | Get attributes for the user                                                                                                                                                                                                                                                                                                                                                                |
| Decide                      | Returns a decision result for a flag key for a user in an OptimizelyDecision object containing all data required to deliver the flag rule. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-csharp)                                                                                                                              |
| DecideForKeys               | Returns a map of flag decisions for specified flag keys. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-csharp)                                                                                                                                                                                                                |
| DecideAll                   | Returns decisions for all active (unarchived) flags for a user. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-csharp)                                                                                                                                                                                                         |
| TrackEvent                  | Tracks a conversion event for a user (an action a user takes) and logs an error message if the specified event key does not match any existing events. See \[Track Event]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/track-event-csharp)                                                                                                                        |
| SetForcedDecision           | Forces a user into a specific variation. See \[Set Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-csharp#set-forced-decision-method---setforceddecision)                                                                                                                                                                   |
| GetForcedDecision           | Returns what variation the user was forced into. See \[Get Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-csharp#get-forced-decision-method---getforceddecision)                                                                                                                                                           |
| RemoveForcedDecision        | Removes a user from a specific forced variation. See \[Remove Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-csharp#remove-forced-decision-method---removeforceddecision)                                                                                                                                                  |
| RemoveAllForcedDecisions    | Removes a user from all forced variations. See \[Remove All Forced Decisions]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-csharp#remove-all-forced-decisions-method---removeallforceddecisions)                                                                                                                                          |
| FetchQualifiedSegments \*\* | Fetch all Optimizely Data Platform (ODP) real-time audiences for which the user context qualified. See \[Real-Time Audiences for Feature Experimentation segment qualification methods for the C# SDK]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-csharp#fetchqualifiedsegments). |
| IsQualifiedFor \*\*         | Check if the user context qualifies for a given ODP real-time segment. See \[Real-Time Audiences for Feature Experimentation segment qualification methods for the C# SDK]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-csharp#fetchqualifiedsegments).                             |

\*\* Requires [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8).

## See also

[Create User Context](docId\:NMLEiJmrOW1XL9kGEplxb)
[Forced Decision methods](docId\:Hvqig-mRzzH9WkU54enfY)

## Source files

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