AppContext::TryGetSwitch Method (String^, Boolean%)

.NET Framework (current version)
 

Tries to get the value of a switch.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public:
static bool TryGetSwitch(
	String^ switchName,
	[OutAttribute] bool% isEnabled
)

Parameters

switchName
Type: System::String^

The name of the switch.

isEnabled
Type: System::Boolean%

When this method returns, contains the value of switchName if switchName was found, or false if switchName was not found. This parameter is passed uninitialized.

Return Value

Type: System::Boolean

true if switchName was set and the isEnabled argument contains the value of the switch; otherwise, false.

Exception Condition
ArgumentNullException

switchName is null.

ArgumentException

switchName is String::Empty.

The AppContext class enables library writers to provide a uniform opt-out mechanism for new functionality for their users. It establishes a loosely-coupled contract between components in order to communicate an opt-out request. This capability is typically important when a change is made to existing functionality. Conversely, there is already an implicit opt-in for new functionality.

The common language runtime automatically populates the switches assigned to an AppContext instance by reading the registry and the application's configuration file. The value of these switches can then be overridden, and new switches added, by calling the SetSwitch method.

A library calls the TryGetSwitch method to check whether its consumers have declared the value of the switch and then act appropriately on it. By default, if the switch is not defined, the new functionality is enabled.. If the switch is defined and its value is false, the new functionality is also enabled. If its value is true, the legacy behavior is enabled.

The following example determines whether a library consumer has set a switch named Switch.AmazingLib.ThrowOnException.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 10
.NET Framework
Available since 4.6
Return to top
Show: