5

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?

Athanviel
  • 199
  • 1
  • 3
  • 15

3 Answers3

10

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
    
Richard
  • 106,783
  • 21
  • 203
  • 265
  • Thanks! I really like editorconfig here is how you create one automatically: https://learn.microsoft.com/en-gb/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2019 manually: https://learn.microsoft.com/de-de/visualstudio/ide/create-portable-custom-editor-options?view=vs-2019 – Athanviel Mar 09 '20 at 12:39
3

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>
Asad
  • 78
  • 7
0

You can use Code Snippet to do that (https://learn.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.

Alex - Tin Le
  • 1,982
  • 1
  • 6
  • 11