-3

When I refactor a class with a regular constructor to a primary constructor I'd like Visual Studio to capitalize the name of the parameters.

For example:

public class Desk
{
    private Guid Material;
  
    public Desk(Guid material)
    {
        Material = material;
    }
}

would become

public class Desk(Guid Material)
{
    // ....  
}

The reason being PascalCase variables are outside the scope of any method. It doesn't matter to the method if the class chooses to make that public or not.

How can I get VS to suggest this sort of refactor when it suggests a primary constructor.

14
  • 3
    They're still private to the class, captured as fields, so they should not be PascalCase, they should still be camelCase. Commented May 29 at 16:07
  • 2
    I think you want public class Desk(Guid material) { private Guid Material { get; } = material; } Commented May 29 at 16:09
  • 2
    Do you name all your private fields in PascalCase, too? There's nothing saying you can't do that, and it's perfectly fine if you're the only one maintaining that code, but that's not in line with .NET naming conventions and others would be confused by it. The refactoring doesn't offer to do that because it' sticks with .NET naming conventions. Commented May 29 at 16:16
  • 1
    Well, that's not "normal" for .NET. Parameters, local variables, and private fields are usually in camelCase. As a team, you're going to be fighting the norm, and new additions to your team are going to say "What the---?!" Why? Commented May 29 at 16:22
  • 1
    At a high level, you create an analyzer project, implement an analyzer to generate a warning, and implement a code fix that changes the syntax tree. You'll need to search for an example or tutorial on Roslyn analyzers and code fixes for details on how to do that. You can use it either as a Visual Studio extension (applies to all projects) or as a NuGet package (can be added per project using PackageReference,, ideally using an internal NuGet source) Commented May 29 at 16:36

1 Answer 1

1

You can apply rules specifically to primary constructor parameters in ReSharper. As per the comment from Egor Fedorenko:

  1. Edit the default "Parameters" rules to NOT to apply on primary constructor parameters:
  2. Create a new rule "Primary constructor parameters" with _lowerCaseNaming with underscore, and apply it for primary constructor parameters only: screenshot showing selection of primary constructor parameter as an option
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.