Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs (C#)

Code before:

            foreach (string propertyName in propertyNames)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    _outputString.Append(_delimiter);
                }

                string value = null;
                if (mshObject.BaseObject is IDictionary dictionary)
                {
                    if (dictionary.Contains(propertyName))
                    {
                        //Handle null reference exception for existing property with null value
                        if (dictionary[propertyName] == null)
                        {
                            value = null;
                        }
                        else
                        {
                            value = dictionary[propertyName].ToString();
                        }
                    }
                    else if (mshObject.Properties[propertyName] is PSPropertyInfo property)
                    {
                        value = GetToStringValueForProperty(property);
                    }
                }
                else if (mshObject.Properties[propertyName] is PSPropertyInfo property)
                {
                    value = GetToStringValueForProperty(property);
                }

                // If value is null, assume property is not present and skip it.
                if (value != null)
                {
                    if (_quoteFields != null)
                    {
                        if (_quoteFields.TryGetValue(propertyName, out _))
                        {
                            AppendStringWithEscapeAlways(_outputString, value);
                        }
                        else
                        {
                            _outputString.Append(value);
                        }
                    }

Review comment (refactor):
Can we simplify the code?
```suggestion
                        value = dictionary[propertyName]?.ToString();
```
Also please remove obvious comment.

Code after:

            foreach (string propertyName in propertyNames)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    _outputString.Append(_delimiter);
                }

                string value = null;
                if (mshObject.BaseObject is IDictionary dictionary)
                {
                    if (dictionary.Contains(propertyName))
                    {
                        value = dictionary[propertyName]?.ToString();
                    }
                    else if (mshObject.Properties[propertyName] is PSPropertyInfo property)
                    {
                        value = GetToStringValueForProperty(property);
                    }
                }
                else if (mshObject.Properties[propertyName] is PSPropertyInfo property)
                {
                    value = GetToStringValueForProperty(property);
                }

                // If value is null, assume property is not present and skip it.
                if (value != null)
                {
                    if (_quoteFields != null)
                    {
                        if (_quoteFields.TryGetValue(propertyName, out _))
                        {
                            AppendStringWithEscapeAlways(_outputString, value);
                        }
                        else
                        {
                            _outputString.Append(value);
                        }
                    }
                    else
                    {
                        switch (_quoteKind)
                        {
                            case BaseCsvWritingCommand.QuoteKind.Always:
                                AppendStringWithEscapeAlways(_outputString, value);
                                break;
                            case BaseCsvWritingCommand.QuoteKind.AsNeeded: