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 addusing
block, but
fornew class
it won't, because, your class doesn't use any
additional refereces which need to add initially, inVisual studio
lastest release for adding new class it won't add anyusing 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:
Since
dotnet 5 and 6
compiler automatically adds a set ofusing directives
based on the project type. You canget 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:
So hope you get the difference already. It’s not an issue, it’s a new
features inlatest 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
Is Using an an 'Async' Lambda with 'Task.Run()' Redundant
How to Get Modified Date from File in C# on Windows Mobile
How Can User Resize Control at Runtime in Winforms
How to Prevent Datagridview from Flickering When Scrolling Horizontally
How to Get Character for a Given Ascii Value
Detecting If Paste Event Occurred Inside a Rich Text Box
Is the Sorting Algorithm Used by .Net's 'Array.Sort()' Method a Stable Algorithm
A Way to Push Buffered Events in Even Intervals
SQL Injection Attack Prevention: Where Do I Start
How to Find Duplicates in an Array and Display How Many Times They Occurred
How to Hide Public Methods from Intellisense
Can't See Localhost from Uwp App
"Nested Foreach" VS "Lambda/Linq Query" Performance(Linq-To-Objects)
Efficient Way to Round Double Precision Numbers to a Lower Precision Given in Number of Bits
Google Maps V3 Geocoding Server-Side
The 'Await' Operator Can Only Be Used Within an Async Lambda Expression