diff --git a/.globalconfig b/.globalconfig index 0b395802754..ddae38c4886 100644 --- a/.globalconfig +++ b/.globalconfig @@ -991,7 +991,7 @@ dotnet_diagnostic.IDE1003.severity = silent dotnet_diagnostic.IDE1004.severity = silent # InvokeDelegateWithConditionalAccess -dotnet_diagnostic.IDE1005.severity = silent +dotnet_diagnostic.IDE1005.severity = warning # NamingRule dotnet_diagnostic.IDE1006.severity = silent diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionProxy.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionProxy.cs index 56e90ad6f8b..c624a867640 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionProxy.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimSessionProxy.cs @@ -779,11 +779,7 @@ private void FireOperationCreatedEvent( OperationEventArgs args = new OperationEventArgs( cancelOperation, operation, false); - OperationEventHandler temp = this.OnOperationCreated; - if (temp != null) - { - temp(this.session, args); - } + this.OnOperationCreated?.Invoke(this.session, args); this.PostOperationCreateEvent(args); } @@ -803,11 +799,7 @@ private void FireOperationDeletedEvent( OperationEventArgs args = new OperationEventArgs( null, operation, success); PreOperationDeleteEvent(args); - OperationEventHandler temp = this.OnOperationDeleted; - if (temp != null) - { - temp(this.session, args); - } + this.OnOperationDeleted?.Invoke(this.session, args); this.PostOperationDeleteEvent(args); this.RemoveOperation(operation); diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs index 9ad44f3a07f..06dcc9150b4 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs @@ -246,10 +246,7 @@ private void HandleJobOutput(Job job, TSession sessionForJob, bool discardNonPip return; } - if (outputAction != null) - { - outputAction(pso); - } + outputAction?.Invoke(pso); }; job.Output.DataAdded += diff --git a/src/System.Management.Automation/engine/EventManager.cs b/src/System.Management.Automation/engine/EventManager.cs index 7dd39180885..012f5c0857c 100644 --- a/src/System.Management.Automation/engine/EventManager.cs +++ b/src/System.Management.Automation/engine/EventManager.cs @@ -1511,12 +1511,7 @@ private Type GenerateEventHandler(MethodInfo invokeSignature) /// protected virtual void OnForwardEvent(PSEventArgs e) { - EventHandler eh = ForwardEvent; - - if (eh != null) - { - eh(this, e); - } + ForwardEvent?.Invoke(this, e); } /// @@ -1824,12 +1819,7 @@ public override void UnsubscribeEvent(PSEventSubscriber subscriber) /// protected virtual void OnForwardEvent(PSEventArgs e) { - EventHandler eh = ForwardEvent; - - if (eh != null) - { - eh(this, e); - } + ForwardEvent?.Invoke(this, e); } } @@ -2067,10 +2057,7 @@ public override int GetHashCode() internal void OnPSEventUnsubscribed(object sender, PSEventUnsubscribedEventArgs e) { - if (Unsubscribed != null) - { - Unsubscribed(sender, e); - } + Unsubscribed?.Invoke(sender, e); } } @@ -2404,11 +2391,7 @@ public PSEventArgs this[int index] private void OnPSEventReceived(object sender, PSEventArgs e) { - PSEventReceivedEventHandler eventHandler = PSEventReceived; - if (eventHandler != null) - { - eventHandler(sender, e); - } + PSEventReceived?.Invoke(sender, e); } /// diff --git a/src/System.Management.Automation/engine/hostifaces/AsyncResult.cs b/src/System.Management.Automation/engine/hostifaces/AsyncResult.cs index 8a0101ae802..fa965f4ab59 100644 --- a/src/System.Management.Automation/engine/hostifaces/AsyncResult.cs +++ b/src/System.Management.Automation/engine/hostifaces/AsyncResult.cs @@ -152,10 +152,7 @@ internal void SetAsCompleted(Exception exception) } // call the user supplied callback - if (Callback != null) - { - Callback(this); - } + Callback?.Invoke(this); } /// diff --git a/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs b/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs index dfd33cc2257..7064f9d8036 100644 --- a/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs +++ b/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs @@ -560,11 +560,7 @@ public void Complete() // A temporary variable is used as the Completed may // reach null (because of -='s) after the null check - EventHandler tempCompleted = Completed; - if (tempCompleted != null) - { - tempCompleted(this, EventArgs.Empty); - } + Completed?.Invoke(this, EventArgs.Empty); } if (raiseDataAdded) @@ -1405,22 +1401,14 @@ private void RaiseDataAddingEvent(Guid psInstanceId, object itemAdded) { // A temporary variable is used as the DataAdding may // reach null (because of -='s) after the null check - EventHandler tempDataAdding = DataAdding; - if (tempDataAdding != null) - { - tempDataAdding(this, new DataAddingEventArgs(psInstanceId, itemAdded)); - } + DataAdding?.Invoke(this, new DataAddingEventArgs(psInstanceId, itemAdded)); } private void RaiseDataAddedEvent(Guid psInstanceId, int index) { // A temporary variable is used as the DataAdded may // reach null (because of -='s) after the null check - EventHandler tempDataAdded = DataAdded; - if (tempDataAdded != null) - { - tempDataAdded(this, new DataAddedEventArgs(psInstanceId, index)); - } + DataAdded?.Invoke(this, new DataAddedEventArgs(psInstanceId, index)); } /// diff --git a/src/System.Management.Automation/engine/hostifaces/RunspacePool.cs b/src/System.Management.Automation/engine/hostifaces/RunspacePool.cs index 9f281f2956a..92da7ade106 100644 --- a/src/System.Management.Automation/engine/hostifaces/RunspacePool.cs +++ b/src/System.Management.Automation/engine/hostifaces/RunspacePool.cs @@ -837,12 +837,7 @@ private void OnInternalPoolForwardEvent(object sender, PSEventArgs e) /// private void OnEventForwarded(PSEventArgs e) { - EventHandler eh = InternalForwardEvent; - - if (eh != null) - { - eh(this, e); - } + InternalForwardEvent?.Invoke(this, e); } /// diff --git a/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs b/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs index 37ab127658c..63f39427a8f 100644 --- a/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs +++ b/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs @@ -1640,12 +1640,7 @@ protected void AssertIfStateIsBeforeOpen() /// protected virtual void OnForwardEvent(PSEventArgs e) { - EventHandler eh = this.ForwardEvent; - - if (eh != null) - { - eh(this, e); - } + this.ForwardEvent?.Invoke(this, e); } /// diff --git a/src/System.Management.Automation/engine/parser/Compiler.cs b/src/System.Management.Automation/engine/parser/Compiler.cs index b08d588fa0e..5c75bfe40f8 100644 --- a/src/System.Management.Automation/engine/parser/Compiler.cs +++ b/src/System.Management.Automation/engine/parser/Compiler.cs @@ -2265,11 +2265,9 @@ private Expression CaptureAstResults( exprs.Add(Expression.Assign(resultList, Expression.New(CachedReflectionInfo.ObjectList_ctor))); exprs.Add(Expression.Assign(s_getCurrentPipe, Expression.New(CachedReflectionInfo.Pipe_ctor, resultList))); exprs.Add(Expression.Call(oldPipe, CachedReflectionInfo.Pipe_SetVariableListForTemporaryPipe, s_getCurrentPipe)); - if (generateRedirectExprs != null) - { - // Add merge redirection expressions if delegate is provided. - generateRedirectExprs(exprs, finallyExprs); - } + + // Add merge redirection expressions if delegate is provided. + generateRedirectExprs?.Invoke(exprs, finallyExprs); exprs.Add(Compile(ast)); diff --git a/src/System.Management.Automation/engine/remoting/client/Job2.cs b/src/System.Management.Automation/engine/remoting/client/Job2.cs index 32923806cce..43f423ad711 100644 --- a/src/System.Management.Automation/engine/remoting/client/Job2.cs +++ b/src/System.Management.Automation/engine/remoting/client/Job2.cs @@ -342,10 +342,7 @@ private void RaiseCompletedHandler(int operation, AsyncCompletedEventArgs eventA #pragma warning disable 56500 try { - if (handler != null) - { - handler(this, eventArgs); - } + handler?.Invoke(this, eventArgs); } catch (Exception exception) { diff --git a/src/System.Management.Automation/engine/remoting/client/ThrottlingJob.cs b/src/System.Management.Automation/engine/remoting/client/ThrottlingJob.cs index efb47508bcc..b1d9c968b10 100644 --- a/src/System.Management.Automation/engine/remoting/client/ThrottlingJob.cs +++ b/src/System.Management.Automation/engine/remoting/client/ThrottlingJob.cs @@ -337,10 +337,7 @@ internal void DisableFlowControlForPendingJobsQueue() while (_actionsForUnblockingChildAdditions.Count > 0) { Action a = _actionsForUnblockingChildAdditions.Dequeue(); - if (a != null) - { - a(); - } + a?.Invoke(); } } } @@ -406,10 +403,7 @@ internal void AddChildJobWithoutBlocking(StartableJob childJob, ChildJobFlags fl } else { - if (jobEnqueuedAction != null) - { - jobEnqueuedAction(); - } + jobEnqueuedAction?.Invoke(); } } @@ -746,10 +740,7 @@ private void childJob_StateChanged(object sender, JobStateEventArgs e) if (_actionsForUnblockingChildAdditions.Count > 0) { Action a = _actionsForUnblockingChildAdditions.Dequeue(); - if (a != null) - { - a(); - } + a?.Invoke(); } if (_cmdletMode) diff --git a/src/System.Management.Automation/engine/remoting/client/remotingprotocolimplementation.cs b/src/System.Management.Automation/engine/remoting/client/remotingprotocolimplementation.cs index 8615bf3398a..8ec88c404e0 100644 --- a/src/System.Management.Automation/engine/remoting/client/remotingprotocolimplementation.cs +++ b/src/System.Management.Automation/engine/remoting/client/remotingprotocolimplementation.cs @@ -444,10 +444,7 @@ private void PerformURIRedirectionStep2(System.Uri newURI) } // raise warning to report the redirection - if (_uriRedirectionHandler != null) - { - _uriRedirectionHandler(newURI); - } + _uriRedirectionHandler?.Invoke(newURI); // start a new connection _transportManager.Redirect(newURI, _connectionInfo); diff --git a/src/System.Management.Automation/engine/remoting/commands/WaitJob.cs b/src/System.Management.Automation/engine/remoting/commands/WaitJob.cs index f023ca168a7..796e97513bd 100644 --- a/src/System.Management.Automation/engine/remoting/commands/WaitJob.cs +++ b/src/System.Management.Automation/engine/remoting/commands/WaitJob.cs @@ -105,10 +105,7 @@ private void InvokeEndProcessingAction() } // Invoke action outside lock. - if (endProcessingAction != null) - { - endProcessingAction(); - } + endProcessingAction?.Invoke(); } private void CleanUpEndProcessing() diff --git a/src/System.Management.Automation/engine/remoting/common/fragmentor.cs b/src/System.Management.Automation/engine/remoting/common/fragmentor.cs index da3446e6cfc..832b89a7562 100644 --- a/src/System.Management.Automation/engine/remoting/common/fragmentor.cs +++ b/src/System.Management.Automation/engine/remoting/common/fragmentor.cs @@ -811,10 +811,7 @@ private void WriteCurrentFragmentAndReset() } // call the callback since we have data available - if (_onDataAvailableCallback != null) - { - _onDataAvailableCallback(data, _currentFragment.IsEndFragment); - } + _onDataAvailableCallback?.Invoke(data, _currentFragment.IsEndFragment); // prepare a new fragment _currentFragment.FragmentId = ++_fragmentId; diff --git a/src/System.Management.Automation/engine/remoting/server/WSManChannelEvents.cs b/src/System.Management.Automation/engine/remoting/server/WSManChannelEvents.cs index 3b1174f0fff..9fbcb9e0cf9 100644 --- a/src/System.Management.Automation/engine/remoting/server/WSManChannelEvents.cs +++ b/src/System.Management.Automation/engine/remoting/server/WSManChannelEvents.cs @@ -30,11 +30,7 @@ public static class WSManServerChannelEvents /// internal static void RaiseShuttingDownEvent() { - EventHandler handler = ShuttingDown; - if (handler != null) - { - handler(null, EventArgs.Empty); - } + ShuttingDown?.Invoke(null, EventArgs.Empty); } /// @@ -42,11 +38,7 @@ internal static void RaiseShuttingDownEvent() /// internal static void RaiseActiveSessionsChangedEvent(ActiveSessionsChangedEventArgs eventArgs) { - EventHandler handler = ActiveSessionsChanged; - if (handler != null) - { - handler(null, eventArgs); - } + ActiveSessionsChanged?.Invoke(null, eventArgs); } #endregion internal members diff --git a/src/System.Management.Automation/utils/ExtensionMethods.cs b/src/System.Management.Automation/utils/ExtensionMethods.cs index 127c9a226cf..223aa793af5 100644 --- a/src/System.Management.Automation/utils/ExtensionMethods.cs +++ b/src/System.Management.Automation/utils/ExtensionMethods.cs @@ -12,18 +12,12 @@ internal static class ExtensionMethods { public static void SafeInvoke(this EventHandler eventHandler, object sender, EventArgs eventArgs) { - if (eventHandler != null) - { - eventHandler(sender, eventArgs); - } + eventHandler?.Invoke(sender, eventArgs); } public static void SafeInvoke(this EventHandler eventHandler, object sender, T eventArgs) where T : EventArgs { - if (eventHandler != null) - { - eventHandler(sender, eventArgs); - } + eventHandler?.Invoke(sender, eventArgs); } }