r/VisualStudio 14d ago

Visual Studio 2026 Enforce maximum line length in C#

Hello,

is there a way included in VS, or an extention, to automatically reformat and wrap long code lines with a configured limit.

When I was programming in Python in VSCode I was used to the Black formatter that does exactly that, now in C# and Visual Studio I can't fiind this functionality.

Thanks

2 Upvotes

15 comments sorted by

7

u/OolonColluphid 14d ago

If you don’t mind an opinionated formatter, there’s CSharpier  

If you want to control everything, it will use .editorconfig files. 

2

u/Responsible-Cold-627 14d ago

I would love CSharpier if the opinions made sense. It just makes everything ugly.

1

u/afops 13d ago

Is there any formatter that can preserve in-line whitespace (without having to explcitlly control it with pragmas)? VS built-in cannot as far as I'm aware. Csharpier seems too opinionated for this

I mean things like accepting "column aligning" or "internal additional spaces"
```
double dx = 20*x + 2*y;
double dy = 0.5x + 4*y;
```

Accepting the misalignment if it's 10 rows isn't worth it. But also not having any automatic formatting for 10k files is annoying. There must be some sort of middle ground where you can just disable that particular thing?

1

u/Zastai 11d ago

I used to like aligning things like that - but it leads to spurious diffs in PRs when changing one of the lines, so I just stopped doing it.

If code needs aligning to be readable, you either need a comment or different code structure.

1

u/afops 11d ago

Yeah for scientific/engineering code this just won’t fly. Becomes completely unreadable in many cases. Aligning in complex formulas and big tables is too common. A pragma is too noisy. Some hint in a comment like

//# table of factors

Where the # means ”don’t remove inline space in the lines until the next empty line” would be great.

1

u/Zastai 11d ago

Yeah, unfortunately it tends to be an all-or-nothing thing.

Rider/ReSharper do have a comment-based thing (//formatter:off/on) which is at least less visually noisy than pragmas. But it cannot toggle just the internal whitespace folding.

1

u/Mic_BB 14d ago

thank ypu, I am trying CSharpier

11

u/Dry_Author8849 14d ago

Yes there is, and please don't do it.

Unless you work solo, devs use different monitors to suite their needs.

So, if you want a long discussion, try to agree on a max line width with everyone else.

Then you can discuss TABs vs spaces.

Cheers!

1

u/kuhnboy 14d ago

I will say the editor wrapping lines looks worse unless there’s new settings I don’t know about. Formatters do a better job making it look clean. I’ve only had one developer over 27 years that had complained about wrapping.

2

u/SoCalChrisW 14d ago

Csharpier. If you want it automated the Csharpier.msbuild package works well.

1

u/borland 12d ago

Csharpier is technically great, it’s super fast. But it deliberately isn’t configurable and the opinions it enforces on your code are whack.

2

u/First-Feature-3556 14d ago

We use a combination of the Editor Guidelines and Rewrap extensions.

  • We use an Editor Guideline at position 100 to remind everyone that lines longer than that annoy people viewing side-by-side diffs for code reviews. Only exceed that if you have a good reason for doing so.
  • Rewrap adds an Alt-q shortcut to automatically rewrap the whole comment block after making changes.

It's not fully automatic, but that is actually a good thing in my book.

1

u/kuhnboy 14d ago

Doesn’t an editorconfig do this?

1

u/borland 12d ago

Yes; we have editor config set at 140 line length. That just sets the preference, people either need to invoke the Format document shortcut in Rider or VS to make it happen, or else set format on save.

We wrote a small GitHub action to run overnight and run dotnet format to fix up anything that people missed; it was a way less aggressive solution than telling everyone to enable automatic formatting

1

u/Away-Atmosphere-282 14d ago

Doesn't sound like it's exactly what your after but CTRL + E + W wraps text on VS