diff --git a/src/System.Management.Automation/utils/CommandDiscoveryExceptions.cs b/src/System.Management.Automation/utils/CommandDiscoveryExceptions.cs index e08d8e1a65..4ce3568cf5 100644 --- a/src/System.Management.Automation/utils/CommandDiscoveryExceptions.cs +++ b/src/System.Management.Automation/utils/CommandDiscoveryExceptions.cs @@ -121,14 +121,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - _errorCategory, - _commandName); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + _errorCategory, + _commandName); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/ExecutionExceptions.cs b/src/System.Management.Automation/utils/ExecutionExceptions.cs index 226a9cb8cd..ed04086101 100644 --- a/src/System.Management.Automation/utils/ExecutionExceptions.cs +++ b/src/System.Management.Automation/utils/ExecutionExceptions.cs @@ -161,14 +161,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - "CmdletInvocationException", - ErrorCategory.NotSpecified, - null); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + "CmdletInvocationException", + ErrorCategory.NotSpecified, + null); return _errorRecord; } @@ -891,14 +888,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - "CallDepthOverflow", - ErrorCategory.InvalidOperation, - CallDepth); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + "CallDepthOverflow", + ErrorCategory.InvalidOperation, + CallDepth); return _errorRecord; } @@ -999,14 +993,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - "CallDepthOverflow", - ErrorCategory.InvalidOperation, - CallDepth); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + "CallDepthOverflow", + ErrorCategory.InvalidOperation, + CallDepth); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/MshArgumentException.cs b/src/System.Management.Automation/utils/MshArgumentException.cs index d9c9ce725e..8bc58e6c42 100644 --- a/src/System.Management.Automation/utils/MshArgumentException.cs +++ b/src/System.Management.Automation/utils/MshArgumentException.cs @@ -121,14 +121,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - ErrorCategory.InvalidArgument, - null); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + ErrorCategory.InvalidArgument, + null); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/MshArgumentNullException.cs b/src/System.Management.Automation/utils/MshArgumentNullException.cs index be20163607..294c91ea43 100644 --- a/src/System.Management.Automation/utils/MshArgumentNullException.cs +++ b/src/System.Management.Automation/utils/MshArgumentNullException.cs @@ -119,14 +119,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - ErrorCategory.InvalidArgument, - null); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + ErrorCategory.InvalidArgument, + null); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/MshArgumentOutOfRangeException.cs b/src/System.Management.Automation/utils/MshArgumentOutOfRangeException.cs index 2bf48e9047..b8db294e99 100644 --- a/src/System.Management.Automation/utils/MshArgumentOutOfRangeException.cs +++ b/src/System.Management.Automation/utils/MshArgumentOutOfRangeException.cs @@ -117,14 +117,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - ErrorCategory.InvalidArgument, - null); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + ErrorCategory.InvalidArgument, + null); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/MshInvalidOperationException.cs b/src/System.Management.Automation/utils/MshInvalidOperationException.cs index f9a65fcf8a..ff238ef20c 100644 --- a/src/System.Management.Automation/utils/MshInvalidOperationException.cs +++ b/src/System.Management.Automation/utils/MshInvalidOperationException.cs @@ -115,14 +115,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - _errorCategory, - _target); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + _errorCategory, + _target); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/MshNotImplementedException.cs b/src/System.Management.Automation/utils/MshNotImplementedException.cs index 6c2dd504cf..e6e1d1b54e 100644 --- a/src/System.Management.Automation/utils/MshNotImplementedException.cs +++ b/src/System.Management.Automation/utils/MshNotImplementedException.cs @@ -98,14 +98,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - ErrorCategory.NotImplemented, - null); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + ErrorCategory.NotImplemented, + null); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/MshNotSupportedException.cs b/src/System.Management.Automation/utils/MshNotSupportedException.cs index 7614080dcf..d7002f9111 100644 --- a/src/System.Management.Automation/utils/MshNotSupportedException.cs +++ b/src/System.Management.Automation/utils/MshNotSupportedException.cs @@ -98,14 +98,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - ErrorCategory.NotImplemented, - null); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + ErrorCategory.NotImplemented, + null); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/MshObjectDisposedException.cs b/src/System.Management.Automation/utils/MshObjectDisposedException.cs index b8d4ab421c..8ceabb8422 100644 --- a/src/System.Management.Automation/utils/MshObjectDisposedException.cs +++ b/src/System.Management.Automation/utils/MshObjectDisposedException.cs @@ -104,14 +104,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - ErrorCategory.InvalidOperation, - null); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + ErrorCategory.InvalidOperation, + null); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/ObjectStream.cs b/src/System.Management.Automation/utils/ObjectStream.cs index 2f4cbef8a0..c9aef1b928 100644 --- a/src/System.Management.Automation/utils/ObjectStream.cs +++ b/src/System.Management.Automation/utils/ObjectStream.cs @@ -609,15 +609,12 @@ namespace System.Management.Automation.Internal lock (_monitorObject) { - if (_readWaitHandle == null) - { - // Create the handle signaled if there are objects in the stream - // or the stream has been closed. The closed scenario addresses - // Pipeline readers that execute asynchronously. Since the pipeline - // may complete with zero objects before the caller objects this - // handle, it will block indefinitely unless it is set. - _readWaitHandle = new ManualResetEvent(_objects.Count > 0 || !_isOpen); - } + // Create the handle signaled if there are objects in the stream + // or the stream has been closed. The closed scenario addresses + // Pipeline readers that execute asynchronously. Since the pipeline + // may complete with zero objects before the caller objects this + // handle, it will block indefinitely unless it is set. + _readWaitHandle ??= new ManualResetEvent(_objects.Count > 0 || !_isOpen); handle = _readWaitHandle; } @@ -642,10 +639,7 @@ namespace System.Management.Automation.Internal lock (_monitorObject) { - if (_writeWaitHandle == null) - { - _writeWaitHandle = new ManualResetEvent(_objects.Count < _capacity || !_isOpen); - } + _writeWaitHandle ??= new ManualResetEvent(_objects.Count < _capacity || !_isOpen); handle = _writeWaitHandle; } @@ -665,17 +659,14 @@ namespace System.Management.Automation.Internal lock (_monitorObject) { - if (_reader == null) - { - // Always return an object reader, even if the stream - // is closed. This is to address requesting the object reader - // after calling Pipeline.Execute(). NOTE: If Execute completes - // without writing data to the output queue, the - // stream will be in the EndOfPipeline state because the - // stream is closed and has zero data. Since this is a valid - // and expected execution path, we don't want to throw an exception. - _reader = new ObjectReader(this); - } + // Always return an object reader, even if the stream + // is closed. This is to address requesting the object reader + // after calling Pipeline.Execute(). NOTE: If Execute completes + // without writing data to the output queue, the + // stream will be in the EndOfPipeline state because the + // stream is closed and has zero data. Since this is a valid + // and expected execution path, we don't want to throw an exception. + _reader ??= new ObjectReader(this); reader = _reader; } @@ -695,17 +686,14 @@ namespace System.Management.Automation.Internal lock (_monitorObject) { - if (_mshreader == null) - { - // Always return an object reader, even if the stream - // is closed. This is to address requesting the object reader - // after calling Pipeline.Execute(). NOTE: If Execute completes - // without writing data to the output queue, the - // stream will be in the EndOfPipeline state because the - // stream is closed and has zero data. Since this is a valid - // and expected execution path, we don't want to throw an exception. - _mshreader = new PSObjectReader(this); - } + // Always return an object reader, even if the stream + // is closed. This is to address requesting the object reader + // after calling Pipeline.Execute(). NOTE: If Execute completes + // without writing data to the output queue, the + // stream will be in the EndOfPipeline state because the + // stream is closed and has zero data. Since this is a valid + // and expected execution path, we don't want to throw an exception. + _mshreader ??= new PSObjectReader(this); reader = _mshreader; } @@ -726,10 +714,7 @@ namespace System.Management.Automation.Internal lock (_monitorObject) { - if (_writer == null) - { - _writer = new ObjectWriter(this) as PipelineWriter; - } + _writer ??= new ObjectWriter(this) as PipelineWriter; writer = _writer; } @@ -1686,10 +1671,7 @@ namespace System.Management.Automation.Internal { lock (_syncObject) { - if (_objectReader == null) - { - _objectReader = new PSDataCollectionReader(this); - } + _objectReader ??= new PSDataCollectionReader(this); } } @@ -1711,11 +1693,8 @@ namespace System.Management.Automation.Internal { lock (_syncObject) { - if (_objectReaderForPipeline == null) - { - _objectReaderForPipeline = - new PSDataCollectionPipelineReader(this, computerName, runspaceId); - } + _objectReaderForPipeline ??= + new PSDataCollectionPipelineReader(this, computerName, runspaceId); } } @@ -1733,10 +1712,7 @@ namespace System.Management.Automation.Internal { lock (_syncObject) { - if (_psobjectReader == null) - { - _psobjectReader = new PSDataCollectionReader(this); - } + _psobjectReader ??= new PSDataCollectionReader(this); } } @@ -1758,11 +1734,8 @@ namespace System.Management.Automation.Internal { lock (_syncObject) { - if (_psobjectReaderForPipeline == null) - { - _psobjectReaderForPipeline = - new PSDataCollectionPipelineReader(this, computerName, runspaceId); - } + _psobjectReaderForPipeline ??= + new PSDataCollectionPipelineReader(this, computerName, runspaceId); } } @@ -1784,10 +1757,7 @@ namespace System.Management.Automation.Internal { lock (_syncObject) { - if (_writer == null) - { - _writer = new PSDataCollectionWriter(this) as PipelineWriter; - } + _writer ??= new PSDataCollectionWriter(this) as PipelineWriter; } } diff --git a/src/System.Management.Automation/utils/ParameterBinderExceptions.cs b/src/System.Management.Automation/utils/ParameterBinderExceptions.cs index 841523f6bd..fcb10737d3 100644 --- a/src/System.Management.Automation/utils/ParameterBinderExceptions.cs +++ b/src/System.Management.Automation/utils/ParameterBinderExceptions.cs @@ -212,10 +212,7 @@ namespace System.Management.Automation _parameterType = parameterType; _typeSpecified = typeSpecified; - if (errorPosition == null) - { - errorPosition = invocationInfo.ScriptPosition; - } + errorPosition ??= invocationInfo.ScriptPosition; if (errorPosition != null) { diff --git a/src/System.Management.Automation/utils/RuntimeException.cs b/src/System.Management.Automation/utils/RuntimeException.cs index 4ba776799e..ec0efd4f06 100644 --- a/src/System.Management.Automation/utils/RuntimeException.cs +++ b/src/System.Management.Automation/utils/RuntimeException.cs @@ -151,14 +151,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - _errorCategory, - _targetObject); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + _errorCategory, + _targetObject); return _errorRecord; } diff --git a/src/System.Management.Automation/utils/SessionStateExceptions.cs b/src/System.Management.Automation/utils/SessionStateExceptions.cs index a08e7615dc..34ad5c7f4e 100644 --- a/src/System.Management.Automation/utils/SessionStateExceptions.cs +++ b/src/System.Management.Automation/utils/SessionStateExceptions.cs @@ -234,14 +234,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - "ProviderInvocationException", - ErrorCategory.NotSpecified, - null); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + "ProviderInvocationException", + ErrorCategory.NotSpecified, + null); return _errorRecord; } @@ -497,14 +494,11 @@ namespace System.Management.Automation { get { - if (_errorRecord == null) - { - _errorRecord = new ErrorRecord( - new ParentContainsErrorRecordException(this), - _errorId, - _errorCategory, - _itemName); - } + _errorRecord ??= new ErrorRecord( + new ParentContainsErrorRecordException(this), + _errorId, + _errorCategory, + _itemName); return _errorRecord; }