3

I have many code styles specified in my .editorconfig file for my C# projects. I would find really useful if all of my classes could be sealed by default (When you create a new one) or at least they would show warning there class is not sealed. Is there a setting for this in the .editorconfig file ?

PS: Please dont explaint if it is/isnt a good idea, there are many such threads on SO, so it would be redundant.

Kebechet
  • 1,461
  • 15
  • 31
  • And how compiler/style analyzer should determine if the class is not sealed intentionally/is a newly one created? – Guru Stron Jul 07 '22 at 14:05
  • Look at this question: https://stackoverflow.com/questions/824555/why-visual-studio-doesnt-create-a-public-class-by-default – JowJoris Jul 07 '22 at 14:08
  • 1
    If the question is how to create a sealed class default - you can either create a new snippet for your IDE (VS, Rider or even for dotnet new) or modify existing one (VS, Rider) – Guru Stron Jul 07 '22 at 14:09

3 Answers3

4

You could edit the visual studio item templates, which it uses when it creates a new file.

I've done this before to change it to default to public classes instead of private(or internal now with visual studio 2022).

This is the location for Community edtion, but the other editions will be similar.

C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class

Note: the files are indifferent places, for ASP.Net core projects for example so you may need to hunt around.

newky2k
  • 419
  • 5
  • 12
  • 1
    This is the worst option of all of them, because your changes won't be preserved between computers or installations. And if you have multiple people, there's nothing to force them to use your templates, so anything they write won't follow your rules. – Blindy Jul 07 '22 at 14:14
4

There are also some .editorconfig preferences, but they work only after installing Meziantou.Analyzer Nuget package into your project. After you build your solution these sealed suggestions will show up.

[*.cs]
dotnet_diagnostic.MA0053.severity = suggestion

# Report public classes without inheritors (default: false)
MA0053.public_class_should_be_sealed = true

# Report class without inheritors even if there is virtual members (default: false)
MA0053.class_with_virtual_member_shoud_be_sealed = true

Source: https://www.meziantou.net/performance-benefits-of-sealed-class.htm


[2023] .NET 7 EDIT:

Now .NET 7 allows you to specify this warning in .editorconfig like:

# Seal internal types
dotnet_diagnostic.CA1852.severity = warning

But it has one requirement, it works only on internal classes

Source: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1852

Kebechet
  • 1,461
  • 15
  • 31
3

The editor config file doesn't handle creating new classes, in VS it's code snippets and templates that do -- the things you select from that huge nested list of file types. If you want those to generate sealed classes, you have to either edit them or create your own.

Another direction you could go is to write a code analyzer and fix provider that makes your classes sealed, provided a set of conditions you get to pick and implement are true. This has the added benefit of applying retroactively to your already written code as well.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 1
    In fact, a quick search points to this already made analyzer that does what you want: https://github.com/edumserrano/roslyn-analyzers. Simply reference the nuget and get fixing! – Blindy Jul 07 '22 at 14:12