Repository: dotnet/roslyn
File: src/Features/Core/Portable/AddConstructorParametersFromMembers/AddConstructorParametersFromMembersCodeRefactoringProvider.AddConstructorParametersCodeAction.cs (C#)

Code before:

                    // Create the initializer expression using the parameter name
                    var initializerExpression = generator.IdentifierName(parameter.Name);
                    
                    // Wrap the initializer expression in an EqualsValueClause
                    var equalsValueClause = generator.SyntaxGeneratorInternal.EqualsValueClause(default, initializerExpression);
                    
                    // Add the initializer to the property using SyntaxGeneratorInternal
                    var newProperty = generator.SyntaxGeneratorInternal.WithPropertyInitializer(propertySyntax, equalsValueClause);
                    propertyEditor.ReplaceNode(propertySyntax, newProperty);
                    break;
                }
            }
            else if (member is IFieldSymbol field)
            {
                foreach (var syntaxRef in field.DeclaringSyntaxReferences)
                {
                    var fieldSyntax = syntaxRef.GetSyntax(cancellationToken);
                    var fieldDocument = solution.GetRequiredDocument(fieldSyntax.SyntaxTree);
                    var fieldEditor = await solutionEditor.GetDocumentEditorAsync(fieldDocument.Id, cancellationToken).ConfigureAwait(false);

                    // Create the initializer expression using the parameter name
                    var initializerExpression = generator.IdentifierName(parameter.Name);
                    
                    // Wrap the initializer expression in an EqualsValueClause for fields
                    var equalsValueClause = generator.SyntaxGeneratorInternal.EqualsValueClause(default, initializerExpression);
                    
                    // Add the initializer to the field - works for both C# and VB via WithInitializer
                    var newField = generator.WithInitializer(fieldSyntax, equalsValueClause);

                    fieldEditor.ReplaceNode(fieldSyntax, newField);
                    break;
                }
            }
        }

        private IEnumerable<SyntaxNode> CreateAssignStatements(ConstructorCandidate constructorCandidate)
        {
            var factory = _document.GetRequiredLanguageService<SyntaxGenerator>();
            for (var i = 0; i < _missingParameters.Length; ++i)
            {
                var memberName = constructorCandidate.MissingMembers[i].Name;
                var parameterName = _missingParameters[i].Name;
                yield return factory.ExpressionStatement(
                    factory.AssignmentStatement(
                        factory.MemberAccessExpression(factory.ThisExpression(), factory.IdentifierName(memberName)),
                        factory.IdentifierName(parameterName)));
            }
        }

        public override string Title

Review comment (suggestion):
@copilot you can 'lift' the initializerExpression and equalsValueClause out of the for-loop entirely and use for both the property and field case

Code after:
            CancellationToken cancellationToken)
        {
            var solution = solutionEditor.OriginalSolution;
            var generator = _document.GetRequiredLanguageService<SyntaxGenerator>();

            if (member.DeclaringSyntaxReferences is not [var syntaxRef, ..])
                return;

            var memberSyntax = syntaxRef.GetSyntax(cancellationToken);
            var memberDocument = solution.GetRequiredDocument(memberSyntax.SyntaxTree);
            var memberEditor = await solutionEditor.GetDocumentEditorAsync(memberDocument.Id, cancellationToken).ConfigureAwait(false);

            var equalsValueClause = generator.SyntaxGeneratorInternal.EqualsValueClause(
                generator.IdentifierName(parameter.Name));

            // Add the initializer to the member - use different methods for properties vs fields
            var newMemberSyntax = member is IPropertySymbol
                ? generator.SyntaxGeneratorInternal.WithPropertyInitializer(memberSyntax, equalsValueClause)
                : generator.WithInitializer(memberSyntax, equalsValueClause);

            memberEditor.ReplaceNode(memberSyntax, newMemberSyntax);
        }

        private IEnumerable<SyntaxNode> CreateAssignStatements()
        {
            var factory = _document.GetRequiredLanguageService<SyntaxGenerator>();
            foreach (var (parameter, fieldOrProperty) in MissingParameters)
            {
                yield return factory.ExpressionStatement(
                    factory.AssignmentStatement(
                        factory.MemberAccessExpression(
                            factory.ThisExpression(),
                            factory.IdentifierName(fieldOrProperty.Name)),
                        factory.IdentifierName(parameter.Name)));
            }
        }

        public override string Title
        {
            get
            {
                var parameters = Constructor.Parameters.Select(p => p.ToDisplayString(SimpleFormat));
                var parameterString = string.Join(", ", parameters);
                var signature = $"{this.Constructor.ContainingType.Name}({parameterString})";

                if (_useSubMenuName)
                    return string.Format(CodeFixesResources.Add_to_0, signature);

                return MissingParameters[0].parameter.IsOptional
                    ? string.Format(FeaturesResources.Add_optional_parameters_to_0, signature)
                    : string.Format(FeaturesResources.Add_parameters_to_0, signature);