diff --git a/src/System.Management.Automation/engine/COM/ComTypeInfo.cs b/src/System.Management.Automation/engine/COM/ComTypeInfo.cs
index 851325e82..eab301db3 100644
--- a/src/System.Management.Automation/engine/COM/ComTypeInfo.cs
+++ b/src/System.Management.Automation/engine/COM/ComTypeInfo.cs
@@ -183,10 +183,7 @@ namespace System.Management.Automation
_properties[strName] = prop;
}
- if (prop != null)
- {
- prop.UpdateFuncDesc(funcdesc, index);
- }
+ prop?.UpdateFuncDesc(funcdesc, index);
}
private void AddMethod(string strName, int index)
@@ -198,10 +195,7 @@ namespace System.Management.Automation
_methods[strName] = method;
}
- if (method != null)
- {
- method.AddFuncDesc(index);
- }
+ method?.AddFuncDesc(index);
}
///
diff --git a/src/System.Management.Automation/engine/CmdletParameterBinderController.cs b/src/System.Management.Automation/engine/CmdletParameterBinderController.cs
index c3de48a43..ee2fec058 100644
--- a/src/System.Management.Automation/engine/CmdletParameterBinderController.cs
+++ b/src/System.Management.Automation/engine/CmdletParameterBinderController.cs
@@ -202,10 +202,7 @@ namespace System.Management.Automation
internal void BindCommandLineParametersNoValidation(Collection arguments)
{
var psCompiledScriptCmdlet = this.Command as PSScriptCmdlet;
- if (psCompiledScriptCmdlet != null)
- {
- psCompiledScriptCmdlet.PrepareForBinding(this.CommandLineParameters);
- }
+ psCompiledScriptCmdlet?.PrepareForBinding(this.CommandLineParameters);
InitUnboundArguments(arguments);
CommandMetadata cmdletMetadata = _commandMetadata;
@@ -1044,10 +1041,7 @@ namespace System.Management.Automation
_commandMetadata.ImplementsDynamicParameters,
"The metadata for the dynamic parameters should only be available if the command supports IDynamicParameters");
- if (_dynamicParameterBinder != null)
- {
- _dynamicParameterBinder.BindParameter(argumentToBind.ParameterName, argumentToBind.ArgumentValue, parameter.Parameter);
- }
+ _dynamicParameterBinder?.BindParameter(argumentToBind.ParameterName, argumentToBind.ArgumentValue, parameter.Parameter);
break;
}
diff --git a/src/System.Management.Automation/engine/CommandCompletion/PseudoParameterBinder.cs b/src/System.Management.Automation/engine/CommandCompletion/PseudoParameterBinder.cs
index 44f18d7c6..97b39cf39 100644
--- a/src/System.Management.Automation/engine/CommandCompletion/PseudoParameterBinder.cs
+++ b/src/System.Management.Automation/engine/CommandCompletion/PseudoParameterBinder.cs
@@ -1222,10 +1222,7 @@ namespace System.Management.Automation.Language
var parameter = _commandElements[commandIndex] as CommandParameterAst;
if (parameter != null)
{
- if (argumentsToGetDynamicParameters != null)
- {
- argumentsToGetDynamicParameters.Add(parameter.Extent.Text);
- }
+ argumentsToGetDynamicParameters?.Add(parameter.Extent.Text);
AstPair parameterArg = parameter.Argument != null
? new AstPair(parameter)
diff --git a/src/System.Management.Automation/engine/CommandInfo.cs b/src/System.Management.Automation/engine/CommandInfo.cs
index 1acca145f..eb2e71634 100644
--- a/src/System.Management.Automation/engine/CommandInfo.cs
+++ b/src/System.Management.Automation/engine/CommandInfo.cs
@@ -467,11 +467,8 @@ namespace System.Management.Automation
processInCurrentThread: true,
waitForCompletionInCurrentThread: true);
- if (eventArgs.Exception != null)
- {
- // An exception happened on a different thread, rethrow it here on the correct thread.
- eventArgs.Exception.Throw();
- }
+ // An exception happened on a different thread, rethrow it here on the correct thread.
+ eventArgs.Exception?.Throw();
return eventArgs.Result;
}
diff --git a/src/System.Management.Automation/engine/EventManager.cs b/src/System.Management.Automation/engine/EventManager.cs
index 3df9153f4..783048192 100644
--- a/src/System.Management.Automation/engine/EventManager.cs
+++ b/src/System.Management.Automation/engine/EventManager.cs
@@ -862,10 +862,7 @@ namespace System.Management.Automation
}
// Stop the job
- if (subscriber.Action != null)
- {
- subscriber.Action.NotifyJobStopped();
- }
+ subscriber.Action?.NotifyJobStopped();
lock (_eventSubscribers)
{
@@ -1532,10 +1529,7 @@ namespace System.Management.Automation
{
lock (_eventSubscribers)
{
- if (_timer != null)
- {
- _timer.Dispose();
- }
+ _timer?.Dispose();
foreach (PSEventSubscriber currentSubscriber in _eventSubscribers.Keys.ToArray())
{
diff --git a/src/System.Management.Automation/engine/ExecutionContext.cs b/src/System.Management.Automation/engine/ExecutionContext.cs
index aa13b2aa7..0f2fb0822 100644
--- a/src/System.Management.Automation/engine/ExecutionContext.cs
+++ b/src/System.Management.Automation/engine/ExecutionContext.cs
@@ -53,22 +53,12 @@ namespace System.Management.Automation
///
internal void ResetManagers()
{
- if (_debugger != null)
- {
- _debugger.ResetDebugger();
- }
-
- if (Events != null)
- {
- Events.Dispose();
- }
+ _debugger?.ResetDebugger();
+ Events?.Dispose();
Events = new PSLocalEventManager(this);
- if (this.transactionManager != null)
- {
- this.transactionManager.Dispose();
- }
+ this.transactionManager?.Dispose();
this.transactionManager = new PSTransactionManager();
}
///
@@ -1155,22 +1145,12 @@ namespace System.Management.Automation
{
EngineSessionState.RunspaceClosingNotification();
- if (_debugger != null)
- {
- _debugger.Dispose();
- }
-
- if (Events != null)
- {
- Events.Dispose();
- }
+ _debugger?.Dispose();
+ Events?.Dispose();
Events = null;
- if (this.transactionManager != null)
- {
- this.transactionManager.Dispose();
- }
+ this.transactionManager?.Dispose();
this.transactionManager = null;
}
diff --git a/src/System.Management.Automation/engine/InternalCommands.cs b/src/System.Management.Automation/engine/InternalCommands.cs
index f86478e74..f1c5f57c8 100644
--- a/src/System.Management.Automation/engine/InternalCommands.cs
+++ b/src/System.Management.Automation/engine/InternalCommands.cs
@@ -919,17 +919,14 @@ namespace Microsoft.PowerShell.Commands
// because it allows you to parameterize a command - for example you might allow
// for actions before and after the main processing script. They could be null
// by default and therefore ignored then filled in later...
- if (_scripts[i] != null)
- {
- _scripts[i].InvokeUsingCmdlet(
- contextCmdlet: this,
- useLocalScope: false,
- errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
- dollarUnder: InputObject,
- input: new object[] { InputObject },
- scriptThis: AutomationNull.Value,
- args: Array.Empty