Forced Decision methods for the C# SDK
Describes the Forced Decision methods for the C# SDK, which you can use to force users into a specific variation in Optimizely Feature Experimentation.
These methods help test and debug various flows of your client applications by forcing users into a specific variation.
The C# SDK checks if anyForced Decision methods were implemented before making any decisions. If a matching item is found for the requested flag, the C# SDK will return the forced decision immediately (audience conditions andtraffic allocations are ignored) before making normal decisions.
The following describes specific scenarios the C# SDK follows:
Flag-to-Decision
- SDK will look up at the beginning of anydecide call for the given flag. If a matching Flag-to-Decision forced decision is found for the flag, it returns the decision.
Experiment-Rule-to-Decision
- SDK will look up at the beginning of the decision for the given experiment rule (of the flag key). If a matching Experiment-Rule-to-Decision forced decision is found for the flag, it returns the decision.
Delivery-Rule-to-Decision
- SDK will look up at the beginning of the decision for the given delivery rule (of the flag key). If a matching Delivery-Rule-to-Decision forced decision is found, it returns the decision.
❗️
WarningYou must associate your variations to aflag rule before calling any Forced Decision methods.
On forced decisions, the C# SDK will fire impression events and notifications just like other normal decisions (unless disabled by thedisableDecisionEvent option).
📘
NoteThese forced decisions are not persistent and are cleared when theOptimizelyUserContext is re-initialized.
For information about each forced decision method, click on the method name below:
OptimizelyDecisionContext
public class OptimizelyDecisionContext { public OptimizelyDecisionContext(string flagKey, string? ruleKey = null); public string FlagKey; public string RuleKey;}OptimizelyForcedDecision
public class OptimizelyForcedDecision { public OptimizelyForcedDecision(string variationKey); public string VariationKey;}Set Forced Decision method –SetForcedDecision()
SetForcedDecision()Version
3.11.0
Description
Sets a forced decision (variationKey) for a givenOptimizelyDecisionContext.
Parameters
This table lists the required and optional parameters for the C# SDK.
Parameter | Type | Description |
|---|---|---|
contextrequired | Class | An instance of |
decision | Class | An instance of |
Returns
A boolean value that indicates if setting the forced decision (variationKey) was completed successfully.
Example
See the full C# SDK example in theFull code example section.
Get Forced Decision method –GetForcedDecision()
GetForcedDecision()Version
3.11.0
Description
Returns the forced decision (variationKey) for a givenOptimizelyDecisionContext. Returns theOptimizelyForcedDecision instance or null if there is no matching item.
Parameters
This table lists the required and optional parameters for the C# SDK.
Parameter | Type | Description |
|---|---|---|
contextrequired | Class | An instance of |
Returns
A forced decisionOptimizelyForcedDecision instance for the context or null if there is no matching item.
Example
See the full C# SDK example in theFull code example section.
Remove Forced Decision method –RemoveForcedDecision()
RemoveForcedDecision()Version
3.11.0
Description
Removes the forced decision (variationKey) for a givenOptimizelyDecisionContext.
Parameters
This table lists the required and optional parameters for the C# SDK.
Parameter | Type | Description |
|---|---|---|
contextrequired | Class | An instance of |
Returns
A success or failure boolean status if the forced decision (variationKey) was removed.
Example
See the full C# SDK example in theFull code example section.
Remove All Forced Decisions method –RemoveAllForcedDecisions()
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 theFull code example section.
Full code example
An example of how to use the C# methods for managing forced decisions
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 flagvar success = user.SetForcedDecision(flagContext, variationAForcedDecision);var decision = user.Decide("flag-1");// set a forced decision for an ab-test rulesuccess = user.SetForcedDecision(flagAndABTestContext, variationBForcedDecision);decision = user.Decide("flag-1");// set a forced variation for a delivery rulesuccess = user.SetForcedDecision(flagAndDeliveryRuleContext, variationOnForcedDecision);decision = user.Decide("flag-1");// get forced variationsvar forcedDecision = user.GetForcedDecision(flagContext);Console.WriteLine(string.Format("ForcedDecision variationKey = {0}", forcedDecision.VariationKey));// remove forced variationssuccess = user.RemoveForcedDecision(flagAndABTestContext);success = user.RemoveAllForcedDecisions();See also
Updated 18 days ago