Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

AppContext::SetSwitch Method (String^, Boolean)

.NET Framework (current version)
 

Sets the value of a switch.

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

public:
static void SetSwitch(
	String^ switchName,
	bool isEnabled
)

Parameters

switchName
Type: System::String^

The name of the switch.

isEnabled
Type: System::Boolean

The value of the switch.

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 SetSwitch method is called by an application (or a library) to declare the value of a switch (which is always a Boolean value) that a dependent library defines. The switch is always implicitly false, which provides the new behavior. Setting the switch to true enables it, which provides the legacy behavior. Explicitly setting the switch to false also provides the new behavior. The dependent library can then check the value of the switch by calling the TryGetSwitch method.

System_CAPS_noteNote

It's beneficial to use a consistent format for switch names, since they are a formal contract exposed by a library. The following are two obvious formats.

  • Switch.namespace.switchname

  • Switch.library.switchname

In addition to setting the value of a switch programmatically, it can also be set:

  • by adding the switch name and value to the <AppContextSwitchOverrides> element in the <runtime> section of an application configuration file. For example, the following defines a switch named Libraries.FPLibrary.UseExactFloatingPointComparison whose value is False.

    
    <configuration>
       <runtime>
          <AppContextSwitchOverrides value="Libraries.FPLibrary.UseExactFloatingPointComparison=false" /> 
       </runtime>
    </configuration>
    
    
  • by adding a string value whose name is the name of the switch to the HKLM\SOFTWARE\Microsoft\.NETFramework\AppContext key in the registry. Its value must be the string representation of a Boolean that can be parsed by the Boolean::Parse method; that is, it must be "True", "true", "False", or "false".

If switchName already exists, its value is overwritten by the isEnabled argument. That is, the most recent call to the SetSwitch method overrides the value defined in the registry, in an app configuration file, or by previous calls to the SetSwitch method.

The following line of code sets a switch named Switch.AmazingLib.ThrowOnException to true, which enables a legacy behavior. The library can then check whether a library consumer has set the value of the switch by calling the TryGetSwitch method.

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:
© 2017 Microsoft