Why Visual Studio Doesn't Create a Public Class by Default

Why Visual Studio doesn't create a public class by default?

Making class internal by default makes perfect sense to me: keep your privates to yourself and only explicitly expose parts which really need to be exposed: everything else is just implementation details and should not be visible to the outside world.

In case you want to test your internal classes, .NET 2.0 onwards introduces a new attribute called InternalsVisibleToAttribute, which

Specifies that types that are ordinarily visible only within the current assembly are visible to another assembly.

If this really annoys you, see %ProgramFiles%\Microsoft Visual Studio 8\Common7 IDE\ItemTemplates\CSharp\1033\Class.zip. This is a template which you can change to suit your needs. ReSharper has similar capability, but it's directly accessible from within th UI.

How do you default a new class to public when creating it in Visual Studio?

You need to modify the file located in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033.

Visual Studio 2010 (10.0) and below: There is a zip file in there called Class.zip. Unpack this, edit the file to put in your public keyword then re-pack it (make sure you backup the original).

After this, make sure VS rebuilds it's cache (which is just the zipfiles unzipped into directories in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache) by opening a Visual Studio command shell and execute the following command:

devenv /installvstemplates

Visual Studio 2012 (11.0) and up:
See the answer by @JochemKempe, as it's much easier to change this now, just by editing a single file (no unzipping or rezipping).

UPDATE: Don't forget to open your preferred text editor with admin privileges before you do any edit.

VS 2022 17.1.3 Empty new class

As said, Try to add new controller class it would add using block, but
for new class it won't, because, your class doesn't use any
additional refereces which need to add initially, in Visual studio
lastest release for adding new class it won't add any using block
since it doesn't has any reference to add.

Note: If you would just try it before dotnet core 3.1 you would see that it would add using block. as below:

enter image description here

Since dotnet 5 and 6compiler automatically adds a set of using directives based on the project type. You can get more information on official document here

"When Adding Class which required class reference"

If you add other then your self define class for example middleware class or startup class it would add using directives as required. See the screenshot below:

enter image description here

So hope you get the difference already. It’s not an issue, it’s a new
features in latest dotnet release to redeem repetitive using
directive monotonously.

Why does visual studio set the access modifier of a new class to internal

Because this is the default access modifier for a class, which is not nested. For further documenation on this, please have a look here.

Visual Studio doesn't put space in between 'internalclass' due to EditorConfig

This is a known issue in Visual Studio. It has been fixed, and the fix should be included in the next Visual Studio release.

How can I change the default Visual Studio C# new class file template?

You could modify the following file:

c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

It contains the template used when you add a new class. Inside the same folder you also have the template for interfaces: Interface.zip so that they are public by default. IIRC a restart of VS is necessary to pick the changes.

Class is private or internal default in .net?

It may seem odd that a class is sometimes internal by default (when top level) and sometimes private (when nested). However, this simply means that all access modifiers are maximally restrictive by default. This also means that almost all usages of the word private are redundant - only those on property accessors are meaningful (they can restrict access to an otherwise non-private property).

So I prefer to think of the default as the consistent one - namely being conceptually private, and the keywords being inconsistent - internal sometimes means keep local to the context, and sometimes means grant access to the entire assembly :-). And private almost always means nothing - it's pure boilerplate which is therefore best left out (except in property accessors).



Related Topics



Leave a reply



Submit