---
title: Forced Decision methods for the C# SDK
slug: feature-experimentation/docs/forced-decision-methods-csharp
docTags: 
createdAt: 2026-08-05T20:39:53.000Z
---

These methods help test and debug various flows of your client applications by forcing users into a specific variation.

The C# SDK checks if any `Forced Decision` methods were implemented before making any decisions. If a matching item is found for the requested flag, the C# SDK returns the forced decision immediately ([audience conditions](docId\:UDBvr5a8WHNkyOqzPhOr9) and traffic allocations are ignored) before making normal decisions.

The C# SDK checks for forced decisions at the start of each decision process. If a matching forced decision is found, it returns the decision immediately.

- **Flag-to-Decision** – The SDK checks at the start of any decide call for the given flag.
- **Experiment-Rule-to-Decision** – The SDK checks at the start of the decision for the given experiment rule of the flag key.
- **Delivery-Rule-to-Decision** – The SDK checks at the start of the decision for the given delivery rule of the flag key.

:::hint{type="danger"}
**Warning**
You must associate your variations to a [flag rule](docId:-yq2-thNRv7-SgwCXpG90) before calling any Forced Decision methods.
:::

On forced decisions, the C# SDK fires impression events and notifications just like other normal decisions (unless disabled by the [disableDecisionEvent](docId\:QB-OuF0F-Tj3XpdYgXX1L) option).

:::hint{type="info"}
**Note**
These forced decisions are not persistent and are cleared when the [OptimizelyUserContext](docId\:UZTTnJ2LeXwXXWe_aZvjr) is re-initialized.
:::

For information about each forced decision method, click on the method name.

- [\`SetForcedDecision()\`](docId\:Hvqig-mRzzH9WkU54enfY)
- [\`GetForcedDecision\`](docId\:Hvqig-mRzzH9WkU54enfY)
- [\`RemoveForcedDecision\`](docId\:Hvqig-mRzzH9WkU54enfY)
- [\`RemoveAllForcedDecisions\`](docId\:Hvqig-mRzzH9WkU54enfY)

### OptimizelyDecisionContext

```csharp
public class OptimizelyDecisionContext {
    public OptimizelyDecisionContext(string flagKey, string? ruleKey = null);
  
    public string FlagKey;
  
    public string RuleKey;
}
```

### OptimizelyForcedDecision

```csharp
public class OptimizelyForcedDecision {
    public OptimizelyForcedDecision(string variationKey);
    
    public string VariationKey;
}
```

## Set Forced Decision method – `SetForcedDecision()`

### Version

3.11.0

### Description

Sets a forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

The following table lists the required and optional parameters for the C# SDK.

| Parameter                  | Type  | Description                                                                                                                                  |
| -------------------------- | ----- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\*   | Class | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to set. |
| **decision**\ \*required\* | Class | An instance of \`OptimizelyForcedDecision\` with the required \`variationKey\` for the forced decision you want to set.                      |

### Returns

A boolean value that indicates if setting the forced decision (`variationKey`) was completed.

### Example

See the full C# SDK example in the [Full code example section](docId\:Hvqig-mRzzH9WkU54enfY).

## Get Forced Decision method – `GetForcedDecision()`

### Version

3.11.0

### Description

Returns the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`. Returns the `OptimizelyForcedDecision` instance or null if there is no matching item.

### Parameters

The following table lists the required and optional parameters for the C# SDK.

| Parameter                | Type  | Description                                                                                                                                  |
| ------------------------ | ----- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\* | Class | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to get. |

### Returns

A forced decision `OptimizelyForcedDecision` instance for the context or null if there is no matching item.

### Example

See the full C# SDK example in the [Full code example section](docId\:Hvqig-mRzzH9WkU54enfY).

## Remove Forced Decision method – `RemoveForcedDecision()`

### Version

3.11.0

### Description

Removes the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the C# SDK.

| Parameter                | Type  | Description                                                                                                                                     |
| ------------------------ | ----- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\* | Class | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to remove. |

### Returns

A success or failure boolean status if the forced decision (`variationKey`) was removed.

### Example

See the full C# SDK example in the [Full code example section](docId\:Hvqig-mRzzH9WkU54enfY).

## Remove All Forced Decisions method – `RemoveAllForcedDecisions()`

### Version

3.11.0

### Description

Removes all forced decisions (`variationKey`) for the user context.

### Parameters

This table lists the required and optional parameters for the C# SDK.

| Parameters | Type | Description |
| ---------- | ---- | ----------- |
| None       | N/A  | N/A         |

### Returns

A success or failure boolean status.

### Example

See the full C# SDK example in the [Full code example section](docId\:Hvqig-mRzzH9WkU54enfY).

## Full code example

An example of how to use the C# methods for managing forced decisions

```csharp
var Optimizely = OptimizelyFactory.NewDefaultInstance("<your sdk key>");
var attributes = new UserAttributes();
var user = Optimizely.CreateUserContext(RandomUserId(), attributes);

var flagContext = new OptimizelyDecisionContext("flag-1", null);
var flagAndABTestContext = new OptimizelyDecisionContext("flag-1", "ab-test-1");
var flagAndDeliveryRuleContext = new OptimizelyDecisionContext("flag-1", "delivery-1");
var variationAForcedDecision = new OptimizelyForcedDecision("variation-a");
var variationBForcedDecision = new OptimizelyForcedDecision("variation-b");
var variationOnForcedDecision = new OptimizelyForcedDecision("on");

// set a forced decision for a flag
var success = user.SetForcedDecision(flagContext, variationAForcedDecision);
var decision = user.Decide("flag-1");

// set a forced decision for an ab-test rule
success = user.SetForcedDecision(flagAndABTestContext, variationBForcedDecision);
decision = user.Decide("flag-1");

// set a forced variation for a delivery rule
success = user.SetForcedDecision(flagAndDeliveryRuleContext, variationOnForcedDecision);
decision = user.Decide("flag-1");

// get forced variations
var forcedDecision = user.GetForcedDecision(flagContext);
Console.WriteLine(string.Format("ForcedDecision variationKey = {0}", forcedDecision.VariationKey));

// remove forced variations
success = user.RemoveForcedDecision(flagAndABTestContext);
success = user.RemoveAllForcedDecisions();
```

## See also

[OptimizelyUserContext](docId\:UZTTnJ2LeXwXXWe_aZvjr)
