'Align const assignments in Visual Studio using .editorconfig

Does anyone know whether it's possible to align const assignments in Visual Studio using .editorconfig.

Just for clarity:

class MyConstants {
    public const string FirstConst = "First constant";
    public const string SecondLongerConstant = "Second longer constant";
}

Should be aligned like this:

class MyConstants {
    public const string FirstConst           = "First constant";
    public const string SecondLongerConstant = "Second longer constant";
}

When I align them manually right now and reformat the file, the code is reset back to the first example.



Solution 1:[1]

In response to: "When I align them manually right now and reformat the file, the code is reset back to the first example".

This won't align them for you, but it does prevent Code Cleanup from resetting them back to your first example (tested in Visual Studio 2022 17.1.1).

csharp_space_around_declaration_statements = ignore

For anyone stumbling across this in search of a more general solution be aware that this works for the question example but does not work for something like assigning to a property or field in a constructor. More information available here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Travis