Generic Environment.GetEnvironmentVariable
TType GetEnvironmentVariable<TType>(string name)
- Throw
ArgumentNullException
whenname
is null. - Throw
InvalidOperationException
when cannot find environment variable byname
. - Throw
InvalidCastException
whenTType
is nullable. - Throw
FormatException
when cannot convert environment variable toTType
. - Throw
SecurityException
when a security error is detected. - Throw
OverflowException
when an arithmetic, casting, or conversion operation in a checked context results in an overflow.
TType
- Type of environment variable value.
name
- Name of environment variable.
TType
value.
// Setup (or get from launchSettings.json)
Environment.SetEnvironmentVariable("YourIntVariable", "12345");
Environment.SetEnvironmentVariable("YourBoolVariable", "true");
Environment.SetEnvironmentVariable("YourStringVariable", "YourStringVariable");
// Valid results
int yourIntVariable = GenericEnvironment.GetEnvironmentVariable<int>("YourIntVariable"); // 12345
bool yourBoolVariable = GenericEnvironment.GetEnvironmentVariable<bool>("YourBoolVariable"); // true
string yourStringVariable = GenericEnvironment.GetEnvironmentVariable<string>("YourStringVariable"); // "YourStringVariable"
// Exceptions
GenericEnvironment.GetEnvironmentVariable<bool>(null); // ArgumentNullException because name parameter is null.
GenericEnvironment.GetEnvironmentVariable<bool>("InvalidName"); // InvalidOperationException because variable not found by name.
GenericEnvironment.GetEnvironmentVariable<bool?>("YourBoolVariable"); // InvalidCastException because type is nullable (bool?)
GenericEnvironment.GetEnvironmentVariable<bool>("YourIntVariable"); // FormatException because cannot convert variable to type (bool -> int)
Try get environment variable (generic Environment.GetEnvironmentVariable).
bool TryGetEnvironmentVariable<TType>(string name, out TType value)
- [true, value=TType] when environment variable was found.
- [false, value=default(TType)] when
name
is null. - [false, value=default(TType)] when environment variable wasn't found.
- [false, value=default(TType)] when cannot convert environment variable to
TType
. - [false, value=default(TType)] when a security error is detected.
- [false, value=default(TType)] when an arithmetic, casting, or conversion operation in a checked context results in an overflow.
TType
- Type of environment variable value.
name
- Name of environment variable
value
- Environment variable value
true if environment variable was gotten by name
; otherwise, false.
// Setup
Environment.SetEnvironmentVariable("IntEnvironmentVariable", "12345"); // or from launchSettings.json
// Code
bool firstResult = GenericEnvironment.TryGetEnvironmentVariable("IntEnvironmentVariable", out int firstValue);
bool secondResult = GenericEnvironment.TryGetEnvironmentVariable("InvalidEnvironmentVariableName", out int secondValue);
// Output
// [firstResult, firstValue] = [true, 12345]
// [secondResult, secondValue] = [false, 0]
Get environment variable or default(TType) (generic Environment.GetEnvironmentVariable).
TType GetEnvironmentVariableOrDefault<TType>(string name)
- TType when environment variable was found.
- default(TType) when
name
is null. - default(TType) when environment variable wasn't found.
- default(TType) when cannot convert environment variable to
TType
. - default(TType) when a security error is detected.
- default(TType) when an arithmetic, casting, or conversion operation in a checked context results in an overflow.
TType
- Type of environment variable value.
name
- Name of environment variable
Environment variable or default(TType).
// Setup
Environment.SetEnvironmentVariable("IntEnvironmentVariable", "12345"); // or from launchSettings.json
// Code
int firstResult = GenericEnvironment.GetEnvironmentVariableOrDefault<int>("IntEnvironmentVariable");
bool secondResult = GenericEnvironment.GetEnvironmentVariableOrDefault<int>("InvalidEnvironmentVariableName");
// Output
// firstResult - 12345
// secondResult - 0