diff --git a/.globalconfig b/.globalconfig
index 2c16bbfdc27..a368b21e0c1 100644
--- a/.globalconfig
+++ b/.globalconfig
@@ -782,7 +782,7 @@ dotnet_diagnostic.IDE0017.severity = silent
dotnet_diagnostic.IDE0018.severity = silent
# IDE0019: InlineAsTypeCheck
-dotnet_diagnostic.IDE0019.severity = silent
+dotnet_diagnostic.IDE0019.severity = warning
# IDE0020: InlineIsTypeCheck
dotnet_diagnostic.IDE0020.severity = silent
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimAsyncOperation.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimAsyncOperation.cs
index 6a74a252959..d6a0dd3c805 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimAsyncOperation.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimAsyncOperation.cs
@@ -348,16 +348,14 @@ protected virtual void SubscribeToCimSessionProxyEvent(CimSessionProxy proxy)
///
protected object GetBaseObject(object value)
{
- PSObject psObject = value as PSObject;
- if (psObject == null)
+ if (value is not PSObject psObject)
{
return value;
}
else
{
object baseObject = psObject.BaseObject;
- var arrayObject = baseObject as object[];
- if (arrayObject == null)
+ if (baseObject is not object[] arrayObject)
{
return baseObject;
}
@@ -384,8 +382,7 @@ protected object GetBaseObject(object value)
/// The object.
protected object GetReferenceOrReferenceArrayObject(object value, ref CimType referenceType)
{
- PSReference cimReference = value as PSReference;
- if (cimReference != null)
+ if (value is PSReference cimReference)
{
object baseObject = GetBaseObject(cimReference.Value);
if (!(baseObject is CimInstance cimInstance))
@@ -398,8 +395,7 @@ protected object GetReferenceOrReferenceArrayObject(object value, ref CimType re
}
else
{
- object[] cimReferenceArray = value as object[];
- if (cimReferenceArray == null)
+ if (value is not object[] cimReferenceArray)
{
return null;
}
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimGetInstance.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimGetInstance.cs
index 8e2973f49ae..b1e3667406a 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimGetInstance.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimGetInstance.cs
@@ -285,8 +285,7 @@ protected static string GetQuery(CimBaseCommand cmdlet)
internal static bool IsClassNameQuerySet(CimBaseCommand cmdlet)
{
DebugHelper.WriteLogEx();
- GetCimInstanceCommand cmd = cmdlet as GetCimInstanceCommand;
- if (cmd != null)
+ if (cmdlet is GetCimInstanceCommand cmd)
{
if (cmd.QueryDialect != null || cmd.SelectProperties != null || cmd.Filter != null)
{
@@ -300,8 +299,7 @@ internal static bool IsClassNameQuerySet(CimBaseCommand cmdlet)
protected static string CreateQuery(CimBaseCommand cmdlet)
{
DebugHelper.WriteLogEx();
- GetCimInstanceCommand cmd = cmdlet as GetCimInstanceCommand;
- if (cmd != null)
+ if (cmdlet is GetCimInstanceCommand cmd)
{
StringBuilder propertyList = new StringBuilder();
if (cmd.SelectProperties == null)
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimIndicationWatcher.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimIndicationWatcher.cs
index 198c86e96af..f8cf387cb8a 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimIndicationWatcher.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimIndicationWatcher.cs
@@ -237,13 +237,11 @@ private void NewSubscriptionResultHandler(object src, CimSubscriptionEventArgs a
if (temp != null)
{
// raise the event
- CimSubscriptionResultEventArgs resultArgs = args as CimSubscriptionResultEventArgs;
- if (resultArgs != null)
+ if (args is CimSubscriptionResultEventArgs resultArgs)
temp(this, new CimIndicationEventInstanceEventArgs(resultArgs.Result));
else
{
- CimSubscriptionExceptionEventArgs exceptionArgs = args as CimSubscriptionExceptionEventArgs;
- if (exceptionArgs != null)
+ if (args is CimSubscriptionExceptionEventArgs exceptionArgs)
temp(this, new CimIndicationEventExceptionEventArgs(exceptionArgs.Exception));
}
}
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimNewCimInstance.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimNewCimInstance.cs
index 14c57c87888..56c8b3c6cc6 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimNewCimInstance.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimNewCimInstance.cs
@@ -180,8 +180,7 @@ internal void GetCimInstance(CimInstance cimInstance, XOperationContextBase cont
{
DebugHelper.WriteLogEx();
- CimNewCimInstanceContext newCimInstanceContext = context as CimNewCimInstanceContext;
- if (newCimInstanceContext == null)
+ if (context is not CimNewCimInstanceContext newCimInstanceContext)
{
DebugHelper.WriteLog("Invalid (null) CimNewCimInstanceContext", 1);
return;
@@ -296,8 +295,7 @@ private CimInstance CreateCimInstance(
DebugHelper.WriteLog("Create and add new property to ciminstance: name = {0}; value = {1}; flags = {2}", 5, propertyName, propertyValue, flag);
- PSReference cimReference = propertyValue as PSReference;
- if (cimReference != null)
+ if (propertyValue is PSReference cimReference)
{
CimProperty newProperty = CimProperty.Create(propertyName, GetBaseObject(cimReference.Value), CimType.Reference, flag);
cimInstance.CimInstanceProperties.Add(newProperty);
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs
index ed547444adc..ca99c6ac967 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimRegisterCimIndication.cs
@@ -216,8 +216,7 @@ private void CimIndicationHandler(object cimSession, CmdletActionEventArgs actio
}
// NOTES: should move after this.Disposed, but need to log the exception
- CimWriteError cimWriteError = actionArgs.Action as CimWriteError;
- if (cimWriteError != null)
+ if (actionArgs.Action is CimWriteError cimWriteError)
{
this.exception = cimWriteError.Exception;
if (!this.ackedEvent.IsSet)
@@ -239,11 +238,9 @@ private void CimIndicationHandler(object cimSession, CmdletActionEventArgs actio
DebugHelper.WriteLog("Got an exception: {0}", 2, exception);
}
- CimWriteResultObject cimWriteResultObject = actionArgs.Action as CimWriteResultObject;
- if (cimWriteResultObject != null)
+ if (actionArgs.Action is CimWriteResultObject cimWriteResultObject)
{
- CimSubscriptionResult result = cimWriteResultObject.Result as CimSubscriptionResult;
- if (result != null)
+ if (cimWriteResultObject.Result is CimSubscriptionResult result)
{
EventHandler temp = this.OnNewSubscriptionResult;
if (temp != null)
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimResultObserver.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimResultObserver.cs
index 3dd51d289aa..4c415711f04 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimResultObserver.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimResultObserver.cs
@@ -437,8 +437,7 @@ public override void OnNext(CimMethodResultBase value)
string resultObjectPSType = null;
PSObject resultObject = null;
- CimMethodResult methodResult = value as CimMethodResult;
- if (methodResult != null)
+ if (value is CimMethodResult methodResult)
{
resultObjectPSType = PSTypeCimMethodResult;
resultObject = new PSObject();
@@ -449,8 +448,7 @@ public override void OnNext(CimMethodResultBase value)
}
else
{
- CimMethodStreamedResult methodStreamedResult = value as CimMethodStreamedResult;
- if (methodStreamedResult != null)
+ if (value is CimMethodStreamedResult methodStreamedResult)
{
resultObjectPSType = PSTypeCimMethodStreamedResult;
resultObject = new PSObject();
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimWriteError.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimWriteError.cs
index 9d367877a4c..3ca2c52c7df 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimWriteError.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimWriteError.cs
@@ -39,14 +39,12 @@ internal static ErrorRecord ErrorRecordFromAnyException(
{
Debug.Assert(inner != null, "Caller should verify inner != null");
- CimException cimException = inner as CimException;
- if (cimException != null)
+ if (inner is CimException cimException)
{
return CreateFromCimException(context, cimException, cimResultContext);
}
- var containsErrorRecord = inner as IContainsErrorRecord;
- if (containsErrorRecord != null)
+ if (inner is IContainsErrorRecord containsErrorRecord)
{
return InitializeErrorRecord(context,
exception: inner,
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CmdletOperation.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CmdletOperation.cs
index 5605ff658f6..8e768e0d6ac 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CmdletOperation.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CmdletOperation.cs
@@ -231,8 +231,7 @@ public override void WriteObject(object sendToPipeline, XOperationContextBase co
if (sendToPipeline is CimInstance)
{
- CimSetCimInstanceContext setContext = context as CimSetCimInstanceContext;
- if (setContext != null)
+ if (context is CimSetCimInstanceContext setContext)
{
if (string.Equals(setContext.ParameterSetName, CimBaseCommand.QueryComputerSet, StringComparison.OrdinalIgnoreCase) ||
string.Equals(setContext.ParameterSetName, CimBaseCommand.QuerySessionSet, StringComparison.OrdinalIgnoreCase))
diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/RegisterCimIndicationCommand.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/RegisterCimIndicationCommand.cs
index ec761587b64..67bf50dce31 100644
--- a/src/Microsoft.Management.Infrastructure.CimCmdlets/RegisterCimIndicationCommand.cs
+++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/RegisterCimIndicationCommand.cs
@@ -273,8 +273,7 @@ private static void newSubscriber_Unsubscribed(
{
DebugHelper.WriteLogEx();
- CimIndicationWatcher watcher = sender as CimIndicationWatcher;
- if (watcher != null)
+ if (sender is CimIndicationWatcher watcher)
{
watcher.Stop();
}
diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs
index 09ac6153a52..3c36b74b512 100644
--- a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs
+++ b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs
@@ -1282,8 +1282,7 @@ private string BuildStructuredQueryFromHashTable(EventLogSession eventLogSession
//
// Build xpath for
//
- Hashtable suppresshash = hash[hashkey_supress_lc] as Hashtable;
- if (suppresshash != null)
+ if (hash[hashkey_supress_lc] is Hashtable suppresshash)
{
xpathStringSuppress = BuildXPathFromHashTable(suppresshash);
}
@@ -1350,8 +1349,7 @@ private string BuildStructuredQueryFromHashTable(EventLogSession eventLogSession
private string HandleEventIdHashValue(object value)
{
StringBuilder ret = new StringBuilder();
- Array idsArray = value as Array;
- if (idsArray != null)
+ if (value is Array idsArray)
{
ret.Append('(');
for (int i = 0; i < idsArray.Length; i++)
@@ -1380,8 +1378,7 @@ private string HandleEventIdHashValue(object value)
private string HandleLevelHashValue(object value)
{
StringBuilder ret = new StringBuilder();
- Array levelsArray = value as Array;
- if (levelsArray != null)
+ if (value is Array levelsArray)
{
ret.Append('(');
for (int i = 0; i < levelsArray.Length; i++)
@@ -1412,8 +1409,7 @@ private string HandleKeywordHashValue(object value)
Int64 keywordsMask = 0;
Int64 keywordLong = 0;
- Array keywordArray = value as Array;
- if (keywordArray != null)
+ if (value is Array keywordArray)
{
foreach (object keyword in keywordArray)
{
@@ -1568,8 +1564,7 @@ private string HandleEndTimeHashValue(object value, Hashtable hash)
private string HandleDataHashValue(object value)
{
StringBuilder ret = new StringBuilder();
- Array dataArray = value as Array;
- if (dataArray != null)
+ if (value is Array dataArray)
{
ret.Append('(');
for (int i = 0; i < dataArray.Length; i++)
@@ -1599,8 +1594,7 @@ private string HandleDataHashValue(object value)
private string HandleNamedDataHashValue(string key, object value)
{
StringBuilder ret = new StringBuilder();
- Array dataArray = value as Array;
- if (dataArray != null)
+ if (value is Array dataArray)
{
ret.Append('(');
for (int i = 0; i < dataArray.Length; i++)
@@ -1847,8 +1841,7 @@ private void CheckHashTablesForNullValues()
}
else
{
- Array eltArray = value as Array;
- if (eltArray != null)
+ if (value is Array eltArray)
{
foreach (object elt in eltArray)
{
diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs
index 24b142a5081..dbf45090884 100644
--- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs
@@ -492,8 +492,7 @@ private IEnumerable GetSessionsToActAgainst(QueryBuilder queryBuilder)
return this.Session;
}
- var sessionBoundQueryBuilder = queryBuilder as ISessionBoundQueryBuilder;
- if (sessionBoundQueryBuilder != null)
+ if (queryBuilder is ISessionBoundQueryBuilder sessionBoundQueryBuilder)
{
TSession sessionOfTheQueryBuilder = sessionBoundQueryBuilder.GetTargetSession();
if (sessionOfTheQueryBuilder != null)
diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/CimJobException.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/CimJobException.cs
index 092103d6fe7..d74b66bb8aa 100644
--- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/CimJobException.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/CimJobException.cs
@@ -104,16 +104,14 @@ internal static CimJobException CreateFromAnyException(
Dbg.Assert(jobContext != null, "Caller should verify jobContext != null");
Dbg.Assert(inner != null, "Caller should verify inner != null");
- CimException cimException = inner as CimException;
- if (cimException != null)
+ if (inner is CimException cimException)
{
return CreateFromCimException(jobDescription, jobContext, cimException);
}
string message = BuildErrorMessage(jobDescription, jobContext, inner.Message);
CimJobException cimJobException = new CimJobException(message, inner);
- var containsErrorRecord = inner as IContainsErrorRecord;
- if (containsErrorRecord != null)
+ if (inner is IContainsErrorRecord containsErrorRecord)
{
cimJobException.InitializeErrorRecord(
jobContext,
@@ -362,8 +360,7 @@ internal bool IsTerminatingError
{
get
{
- var cimException = this.InnerException as CimException;
- if ((cimException == null) || (cimException.ErrorData == null))
+ if ((this.InnerException is not CimException cimException) || (cimException.ErrorData == null))
{
return false;
}
diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/ExtrinsicMethodInvocationJob.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/ExtrinsicMethodInvocationJob.cs
index 990c31c73a5..194a89365eb 100644
--- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/ExtrinsicMethodInvocationJob.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/ExtrinsicMethodInvocationJob.cs
@@ -66,8 +66,7 @@ private void ProcessOutParameter(CimMethodResult methodResult, MethodParameter m
methodParameter.Value = dotNetValue;
cmdletOutput.Add(methodParameter.Name, methodParameter);
- var cimInstances = dotNetValue as CimInstance[];
- if (cimInstances != null)
+ if (dotNetValue is CimInstance[] cimInstances)
{
foreach (var instance in cimInstances)
{
@@ -75,8 +74,7 @@ private void ProcessOutParameter(CimMethodResult methodResult, MethodParameter m
}
}
- var cimInstance = dotNetValue as CimInstance;
- if (cimInstance != null)
+ if (dotNetValue is CimInstance cimInstance)
{
CimCmdletAdapter.AssociateSessionOfOriginWithInstance(cimInstance, this.JobContext.Session);
}
@@ -191,15 +189,13 @@ public override void OnNext(CimMethodResultBase item)
this.ExceptionSafeWrapper(
delegate
{
- var methodResult = item as CimMethodResult;
- if (methodResult != null)
+ if (item is CimMethodResult methodResult)
{
this.OnNext(methodResult);
return;
}
- var streamedResult = item as CimMethodStreamedResult;
- if (streamedResult != null)
+ if (item is CimMethodStreamedResult streamedResult)
{
this.OnNext(streamedResult);
return;
diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/TerminatingErrorTracker.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/TerminatingErrorTracker.cs
index 73b8d0f3015..b9283207538 100644
--- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/TerminatingErrorTracker.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/TerminatingErrorTracker.cs
@@ -53,8 +53,7 @@ private static int GetNumberOfSessions(InvocationInfo invocationInfo)
int maxNumberOfSessionsIndicatedByCimInstanceArguments = 1;
foreach (object cmdletArgument in invocationInfo.BoundParameters.Values)
{
- CimInstance[] array = cmdletArgument as CimInstance[];
- if (array != null)
+ if (cmdletArgument is CimInstance[] array)
{
int numberOfSessionsAssociatedWithArgument = array
.Select(CimCmdletAdapter.GetSessionOfOriginFromCimInstance)
diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs
index 3e885cf60ce..d3ed3221117 100644
--- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs
@@ -629,8 +629,7 @@ internal void ReportJobFailure(IContainsErrorRecord exception)
}
else
{
- CimJobException cje = exception as CimJobException;
- if ((cje != null) && (cje.IsTerminatingError))
+ if ((exception is CimJobException cje) && (cje.IsTerminatingError))
{
terminatingErrorTracker.MarkSessionAsTerminated(this.JobContext.Session, out sessionWasAlreadyTerminated);
isThisTerminatingError = true;
@@ -1019,8 +1018,7 @@ internal static bool IsShowComputerNameMarkerPresent(CimInstance cimInstance)
internal static void AddShowComputerNameMarker(PSObject pso)
{
- PSPropertyInfo psShowComputerNameProperty = pso.InstanceMembers[RemotingConstants.ShowComputerNameNoteProperty] as PSPropertyInfo;
- if (psShowComputerNameProperty != null)
+ if (pso.InstanceMembers[RemotingConstants.ShowComputerNameNoteProperty] is PSPropertyInfo psShowComputerNameProperty)
{
psShowComputerNameProperty.Value = true;
}
diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/clientSideQuery.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/clientSideQuery.cs
index 4159cc07685..95d64acec40 100644
--- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/clientSideQuery.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/clientSideQuery.cs
@@ -36,8 +36,7 @@ public NotFoundError(string propertyName, object propertyValue, bool wildcardsEn
if (wildcardsEnabled)
{
- var propertyValueAsString = propertyValue as string;
- if ((propertyValueAsString != null) && (WildcardPattern.ContainsWildcardCharacters(propertyValueAsString)))
+ if ((propertyValue is string propertyValueAsString) && (WildcardPattern.ContainsWildcardCharacters(propertyValueAsString)))
{
this.ErrorMessageGenerator =
(queryDescription, className) => GetErrorMessageForNotFound_ForWildcard(this.PropertyName, this.PropertyValue, className);
@@ -466,8 +465,7 @@ protected override BehaviorOnNoMatch GetDefaultBehaviorWhenNoMatchesFound(object
}
else
{
- string expectedPropertyValueAsString = cimTypedExpectedPropertyValue as string;
- if (expectedPropertyValueAsString != null && WildcardPattern.ContainsWildcardCharacters(expectedPropertyValueAsString))
+ if (cimTypedExpectedPropertyValue is string expectedPropertyValueAsString && WildcardPattern.ContainsWildcardCharacters(expectedPropertyValueAsString))
{
return BehaviorOnNoMatch.SilentlyContinue;
}
@@ -504,8 +502,7 @@ private static bool NonWildcardEqual(string propertyName, object actualPropertyV
actualPropertyValue = actualPropertyValue.ToString();
}
- var expectedPropertyValueAsString = expectedPropertyValue as string;
- if (expectedPropertyValueAsString != null)
+ if (expectedPropertyValue is string expectedPropertyValueAsString)
{
var actualPropertyValueAsString = (string)actualPropertyValue;
return actualPropertyValueAsString.Equals(expectedPropertyValueAsString, StringComparison.OrdinalIgnoreCase);
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs
index e19a135749a..f34b0ab046d 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs
@@ -913,8 +913,7 @@ internal bool DoStartService(ServiceController serviceController)
}
catch (InvalidOperationException e)
{
- Win32Exception eInner = e.InnerException as Win32Exception;
- if (eInner == null
+ if (e.InnerException is not Win32Exception eInner
|| NativeMethods.ERROR_SERVICE_ALREADY_RUNNING != eInner.NativeErrorCode)
{
exception = e;
@@ -1030,9 +1029,7 @@ internal List DoStopService(ServiceController serviceControll
}
catch (InvalidOperationException e)
{
- Win32Exception eInner =
- e.InnerException as Win32Exception;
- if (eInner == null
+ if (e.InnerException is not Win32Exception eInner
|| NativeMethods.ERROR_SERVICE_NOT_ACTIVE != eInner.NativeErrorCode)
{
exception = e;
@@ -1127,8 +1124,7 @@ internal bool DoPauseService(ServiceController serviceController)
}
catch (InvalidOperationException e)
{
- Win32Exception eInner = e.InnerException as Win32Exception;
- if (eInner != null
+ if (e.InnerException is Win32Exception eInner
&& NativeMethods.ERROR_SERVICE_NOT_ACTIVE == eInner.NativeErrorCode)
{
serviceNotRunning = true;
@@ -1208,8 +1204,7 @@ internal bool DoResumeService(ServiceController serviceController)
}
catch (InvalidOperationException e)
{
- Win32Exception eInner = e.InnerException as Win32Exception;
- if (eInner != null
+ if (e.InnerException is Win32Exception eInner
&& NativeMethods.ERROR_SERVICE_NOT_ACTIVE == eInner.NativeErrorCode)
{
serviceNotRunning = true;
diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddMember.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddMember.cs
index 4fda5a09dd8..337f7eff451 100644
--- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddMember.cs
+++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddMember.cs
@@ -557,9 +557,8 @@ private sealed class ValidateNotePropertyNameAttribute : ValidateArgumentsAttrib
{
protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics)
{
- string notePropertyName = arguments as string;
PSMemberTypes memberType;
- if (notePropertyName != null && LanguagePrimitives.TryConvertTo(notePropertyName, out memberType))
+ if (arguments is string notePropertyName && LanguagePrimitives.TryConvertTo(notePropertyName, out memberType))
{
switch (memberType)
{
diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertTo-Html.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertTo-Html.cs
index e118aeaf41b..cf77bdfd33e 100644
--- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertTo-Html.cs
+++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertTo-Html.cs
@@ -505,9 +505,8 @@ protected override void BeginProcessing()
MshCommandRuntime mshCommandRuntime = this.CommandRuntime as MshCommandRuntime;
string Message = StringUtil.Format(ConvertHTMLStrings.MetaPropertyNotFound, s, _meta[s]);
WarningRecord record = new WarningRecord(Message);
- InvocationInfo invocationInfo = GetVariableValue(SpecialVariables.MyInvocation) as InvocationInfo;
- if (invocationInfo != null)
+ if (GetVariableValue(SpecialVariables.MyInvocation) is InvocationInfo invocationInfo)
{
record.SetInvocationInfo(invocationInfo);
}
@@ -556,16 +555,14 @@ private void WriteColumns(List mshParams)
foreach (MshParameter p in mshParams)
{
COLTag.Append(" reader = sender as PipelineReader;
- if (reader != null && reader.IsOpen)
+ if (sender is PipelineReader reader && reader.IsOpen)
{
WritePipelineCollection(reader.NonBlockingRead(), PSStreamObjectType.Output);
}
@@ -449,8 +447,7 @@ private void HandlePipelineOutputDataReady(object sender, EventArgs e)
private void HandlePipelineErrorDataReady(object sender, EventArgs e)
{
- PipelineReader