Skip to Content
SDKC# / .NETEvaluate Flags

GetFlag(key, context)

Returns the data for a specific flag, or null if the flag does not exist.

var flag = client.GetFlag("new-checkout", new FlagEvaluationContext { UserId = "user-42", }); // FlagData? or null

The UserId in the context is used to evaluate rollout flags consistently: the same user always gets the same result. When the context or UserId is empty, a per-instance anonymous ID is used.

Safe check

if (flag?.Enabled == true) { // Flag is enabled for this user }

GetFlags(context)

Returns all flags for the environment as a read-only list.

var flags = client.GetFlags(new FlagEvaluationContext { UserId = "user-42" }); foreach (var flag in flags) { Console.WriteLine($"{flag.Key}: {flag.Enabled}"); }

Examples by flag type

Boolean flag

var maintenance = client.GetFlag("maintenance-mode", context); if (maintenance?.Enabled == true) { return MaintenancePage(); }

Rollout flag

var newDashboard = client.GetFlag("new-dashboard", context); // flag.Enabled is true or false based on userId and percentage if (newDashboard?.Enabled == true) { return NewDashboard(); } // To see the configured percentage: if (newDashboard?.Type == "rollout") { Console.WriteLine($"{newDashboard.Percent}% of users will see this feature"); }

Before InitAsync()

If you call GetFlag() before InitAsync(), the flag will return null (the local cache is still empty).

var client = new CanaryGateClient("cg_key"); // Do not do this: client.GetFlag("feature"); // null // Do this instead: await client.InitAsync(); client.GetFlag("feature"); // FlagData? or null

Flags that do not exist

If the flag does not exist in the environment, GetFlag() returns null. Always use the null-safe pattern to avoid breaking the application flow:

// If 'new-feature' does not exist in the environment, enabled will be false var flag = client.GetFlag("new-feature"); var enabled = flag?.Enabled ?? false;
Last updated on