diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs
index 24b9332bde5..c08fa0c44ea 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs
@@ -149,10 +149,8 @@ public void RegisterCimIndication(
uint operationTimeout)
{
DebugHelper.WriteLogEx("queryDialect = '{0}'; queryExpression = '{1}'", 0, queryDialect, queryExpression);
- if (cimSession == null)
- {
- throw new ArgumentNullException(string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession)));
- }
+
+ ArgumentNullException.ThrowIfNull(cimSession, string.Format(CultureInfo.CurrentUICulture, CimCmdletStrings.NullArgument, nameof(cimSession)));
this.TargetComputerName = cimSession.ComputerName;
CimSessionProxy proxy = CreateSessionProxy(cimSession, operationTimeout);
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs
index 955d7f8613f..61afc00a676 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs
@@ -371,10 +371,7 @@ internal static class ValidationHelper
///
public static void ValidateNoNullArgument(object obj, string argumentName)
{
- if (obj == null)
- {
- throw new ArgumentNullException(argumentName);
- }
+ ArgumentNullException.ThrowIfNull(obj, argumentName);
}
///
diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs
index b0839c6ccd2..4bc9ebef28d 100644
--- a/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs
+++ b/src/Microsoft.Management.UI.Internal/ManagementList/Common/WpfHelp.cs
@@ -214,10 +214,7 @@ public static void AddChild(FrameworkElement parent, FrameworkElement element)
{
ArgumentNullException.ThrowIfNull(element);
- if (parent == null)
- {
- throw new ArgumentNullException("element");
- }
+ ArgumentNullException.ThrowIfNull(parent, nameof(element));
ContentControl parentContentControl = parent as ContentControl;
@@ -370,10 +367,7 @@ public static T FindVisualAncestorData(this DependencyObject obj)
/// The specified value is a null reference.
public static T FindVisualAncestor(this DependencyObject @object) where T : class
{
- if (@object == null)
- {
- throw new ArgumentNullException("object");
- }
+ ArgumentNullException.ThrowIfNull(@object, nameof(@object));
DependencyObject parent = VisualTreeHelper.GetParent(@object);
diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs
index cf85d79ae36..2d590904097 100644
--- a/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs
+++ b/src/Microsoft.Management.UI.Internal/ManagementList/ManagementList/DefaultStringConverter.cs
@@ -62,7 +62,9 @@ public string DefaultValue
///
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
- if (values == null || values.Length != 1)
+ ArgumentNullException.ThrowIfNull(values);
+
+ if (values.Length != 1)
{
throw new ArgumentNullException("values");
}
diff --git a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs
index da51550c084..d2899b994d6 100644
--- a/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs
+++ b/src/Microsoft.Management.UI.Internal/ShowCommand/ViewModel/AllModulesViewModel.cs
@@ -79,7 +79,9 @@ public class AllModulesViewModel : INotifyPropertyChanged
/// Commands to show.
public AllModulesViewModel(Dictionary importedModules, IEnumerable commands)
{
- if (commands == null || !commands.GetEnumerator().MoveNext())
+ ArgumentNullException.ThrowIfNull(commands);
+
+ if (!commands.GetEnumerator().MoveNext())
{
throw new ArgumentNullException("commands");
}
diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs
index 58a1fbb4c2d..8da4d7f4c78 100644
--- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs
@@ -82,7 +82,8 @@ protected TSession[] Session
set
{
- _session = value ?? throw new ArgumentNullException(nameof(value));
+ ArgumentNullException.ThrowIfNull(value);
+ _session = value;
_sessionWasSpecified = true;
}
}
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
index e6b33142457..e11be68156a 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
@@ -575,10 +575,12 @@ private void ProcessMTUSize(string targetNameOrAddress)
}
else
{
+ ArgumentNullException.ThrowIfNull(replyResult);
+
WriteObject(new PingMtuStatus(
Source,
resolvedTargetName,
- replyResult ?? throw new ArgumentNullException(nameof(replyResult)),
+ replyResult,
CurrentMTUSize));
}
}
diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs
index 50a88bb6754..a6a2a6290b0 100644
--- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs
+++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs
@@ -189,7 +189,7 @@ internal override void ProcessResponse(HttpResponseMessage response)
private static RestReturnType CheckReturnType(HttpResponseMessage response)
{
- if (response == null) { throw new ArgumentNullException(nameof(response)); }
+ ArgumentNullException.ThrowIfNull(response);
RestReturnType rt = RestReturnType.Detect;
string contentType = ContentHelper.GetContentType(response);
diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
index c3e2e196cbb..9e555bd428b 100644
--- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
+++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
@@ -833,10 +833,7 @@ internal void Parse(string[] args)
for (int i = 0; i < args.Length; i++)
{
- if (args[i] is null)
- {
- throw new ArgumentNullException(nameof(args), CommandLineParameterParserStrings.NullElementInArgs);
- }
+ ArgumentNullException.ThrowIfNull(args[i], CommandLineParameterParserStrings.NullElementInArgs);
}
// Indicates that we've called this method on this instance, and that when it's done, the state variables
diff --git a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs
index 94eb340b21c..1c82891b654 100644
--- a/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs
+++ b/src/Microsoft.PowerShell.CoreCLR.Eventing/DotNetCode/Eventing/EventProviderTraceListener.cs
@@ -41,8 +41,7 @@ public string Delimiter
[SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly")]
set
{
- if (value == null)
- throw new ArgumentNullException(nameof(Delimiter));
+ ArgumentNullException.ThrowIfNull(value, nameof(Delimiter));
if (value.Length == 0)
throw new ArgumentException(DotNetEventingStrings.Argument_NeedNonemptyDelimiter);
diff --git a/src/Microsoft.WSMan.Management/ConfigProvider.cs b/src/Microsoft.WSMan.Management/ConfigProvider.cs
index de99812b01d..98c76e31c89 100644
--- a/src/Microsoft.WSMan.Management/ConfigProvider.cs
+++ b/src/Microsoft.WSMan.Management/ConfigProvider.cs
@@ -3413,7 +3413,7 @@ private static string NormalizePath(string path, string host)
///
private PSObject GetItemValue(string path)
{
- if (string.IsNullOrEmpty(path) || (path.Length == 0))
+ if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(path);
}
diff --git a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs
index 2e4e7fe0f9b..5cd38cec25b 100644
--- a/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs
+++ b/src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs
@@ -580,7 +580,9 @@ public static class PowerShellAssemblyLoadContextInitializer
public static void SetPowerShellAssemblyLoadContext([MarshalAs(UnmanagedType.LPWStr)] string basePaths)
{
if (string.IsNullOrEmpty(basePaths))
+ {
throw new ArgumentNullException(nameof(basePaths));
+ }
PowerShellAssemblyLoadContext.InitializeSingleton(basePaths);
}
diff --git a/src/System.Management.Automation/engine/ComInterop/Helpers.cs b/src/System.Management.Automation/engine/ComInterop/Helpers.cs
index 513c3126476..814e93825a7 100644
--- a/src/System.Management.Automation/engine/ComInterop/Helpers.cs
+++ b/src/System.Management.Automation/engine/ComInterop/Helpers.cs
@@ -35,10 +35,7 @@ internal static class Requires
[System.Diagnostics.Conditional("DEBUG")]
internal static void NotNull(object value, string paramName)
{
- if (value == null)
- {
- throw new ArgumentNullException(paramName);
- }
+ ArgumentNullException.ThrowIfNull(value, paramName);
}
[System.Diagnostics.Conditional("DEBUG")]
diff --git a/src/System.Management.Automation/engine/DefaultCommandRuntime.cs b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs
index 48d74667dae..88a2a2ee427 100644
--- a/src/System.Management.Automation/engine/DefaultCommandRuntime.cs
+++ b/src/System.Management.Automation/engine/DefaultCommandRuntime.cs
@@ -21,8 +21,7 @@ internal class DefaultCommandRuntime : ICommandRuntime2
///
public DefaultCommandRuntime(List