Setting to explicitly write default access modifier in Visual Studio

Viewed 1616

When I generate a new class, I would like to have the default access modifier written down explicitly like:

internal class Foo
{
}

instead of:

class Foo
{
}

Is that possible with a setting and if so - how?

3 Answers

Two things you can do:

  1. Modify the class file template. This is found in you VS installation:

    <InstallRoot>\<Edition>\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
    
  2. To confirm something has been specified: add the following to a .editorconfig:

    dotnet_style_require_accessibility_modifiers=always:suggestion
    

It can be done by editing the snippets of Class of your installed visual studio.

  • Press Ctrl+K, Ctrl+B a code snippets manager will open
  • navigate to the location present in location text filed and edit the file with name class.snippet in text editor and change line
    enter image description here
<Code Language="csharp"><![CDATA[ class $name$
          {
              $selected$$end$
          }]]>
                  </Code>

to

  <Code Language="csharp"><![CDATA[internal  class $name$
      {
          $selected$$end$
      }]]>
              </Code>

You can use Code Snippet to do that (https://docs.microsoft.com/en-us/visualstudio/ide/code-snippets?view=vs-2019)

Code snippets are small blocks of reusable code that can be inserted in a code file using a right-click menu (context menu) command or a combination of hotkeys. They typically contain commonly used code blocks such as try-finally or if-else blocks, but they can be used to insert entire classes or methods.

Related