Skip to content

Instantly share code, notes, and snippets.

@richlander
Created April 29, 2015 22:33
Show Gist options
  • Save richlander/7ab29fe9ffa71eabe9be to your computer and use it in GitHub Desktop.
Save richlander/7ab29fe9ffa71eabe9be to your computer and use it in GitHub Desktop.
App Context Usage
bool shouldThrow;
if (!AppContext.TryGetSwitch(“Switch.AmazingLib.ThrowOnException”, out shouldThrow))
{
// The switch value was not set by the application.
// As a result, the value is 'false'. A false value implies the latest behavior.
// The library can declare a default value for a switch based on a condition
// Example: https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/AppContext/AppContextDefaultValues.Defaults.cs
}
// The library can use the value of shouldThrow to throw exceptions or not.
if (shouldThrow)
{
// old code
}
else
{
//new code
}
@atifaziz
Copy link

Fixing those curly-quotes on line #3 to straight ones:

bool shouldThrow;

if (!AppContext.TryGetSwitch("Switch.AmazingLib.ThrowOnException", out shouldThrow))
{
    // The switch value was not set by the application. 
    // As a result, the value is 'false'. A false value implies the latest behavior.

    // The library can declare a default value for a switch based on a condition
    // Example: https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/AppContext/AppContextDefaultValues.Defaults.cs
}

// The library can use the value of shouldThrow to throw exceptions or not.
if (shouldThrow)
{
    // old code
}
else
{
    //new code
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment