0

When we create a new c# class in Visual Studio, it always has an internal access modifier; why can't it be public? enter image description here

Maybe I'm missing something, but when we define entities (POCO) that are exposed to other projects, we have to change the accessibility level from internal to public, or is there a way to change that behaviour of VS?

  • 2
    This is the default because it's considered best practice to start with `internal` and only make things `public` if you really need to. A large public API means that changes later on have to be more careful to not break the public API that other code might depend on. – Logarr May 02 '23 at 16:58

1 Answers1

0

Every item you create in Visual Studio is done by using something called a "template" file. While you can define your own templates for pretty much any file, there are standard templates already shipped with VS, e.g. a C# class.

If you search the following directory:

%ProgramFiles%\Microsoft Visual Studio{year}{edition}\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\

You will find a file "Class.cs". This is the template file VS is using when you selected "Class" as a new item to add to your project.

You can now change this file according to your needs or create a new item template.

Axs
  • 159
  • 4
  • In light of Logarr's [comment](https://stackoverflow.com/questions/76157103/default-accessibility-level-of-a-new-class#comment134305386_76157103), this should not be done. – madreflection May 02 '23 at 17:00
  • @madreflection While I agree with you and Logarr, the question OP asked was if the behavior of VS can be changed to do what OP wants, not if it's a good idea to do so. – Axs May 02 '23 at 17:02
  • True, you answered the question that was asked. It still needed the caveat, though. – madreflection May 02 '23 at 17:05
  • Thank you @logarr, axs for responses, what about your thoughts on making default accessibility as public for example in standard library projects and if required changing access modifier to more restricted. – Mayank.kharyal May 02 '23 at 17:57