72

In Visual Studio when you add a new class, it always created with no modifiers and that makes class internal.

class MyClass
{
}

I would prefer that my class by default is created as public one.

Why is it internal by default?

What would you prefer?

Vadim
  • 21,044
  • 18
  • 65
  • 101
  • 13
    Now if only it was sealed per default as well it would be perfect. – Brian Rasmussen May 05 '09 at 12:21
  • 1
    This is not a duplicate question to the one marked. This question specifically asks why, not how to change it. – Syndog Apr 25 '17 at 13:28
  • If you want to modify the default behavior, check out this question/answer... [http://stackoverflow.com/questions/700086/how-do-you-default-a-new-class-to-public-when-creating-it-in-visual-studio](http://stackoverflow.com/questions/700086/how-do-you-default-a-new-class-to-public-when-creating-it-in-visual-studio) – Steve Dignan May 05 '09 at 12:06
  • But be consciously aware of the potential implications of this... – BenAlabaster May 05 '09 at 12:24
  • 4
    The only implication is that new classes will be public... – Gromer Aug 08 '12 at 21:19

9 Answers9

68

To create a Public class by default for Visual Studio 2012:

Edit this file: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

(Visual Studio 2022: C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class)

To look like this:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
    public class $safeitemrootname$
    {
    }
}

More info: http://aaron-hoffman.blogspot.com/2013/05/edit-default-visual-studio-2012-item.html

Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97
Aaron Hoffman
  • 6,604
  • 8
  • 56
  • 61
43

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.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • 23
    ReSharper by default creates public class. And that makes perfect sense to me. – Vadim May 05 '09 at 12:10
  • You can use InternalsVisbleToAttribute only with strong-signed assemblies. – Vadim May 05 '09 at 12:13
  • 15
    Anton, can you be more specific. It's not going to be the first time I'm wrong. I just want to know what I'm wrong about. – Vadim May 05 '09 at 12:42
  • 1
    Sorry, was in a hurry. See this page (http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx ) for more info on this attribute. – Anton Gogolev May 05 '09 at 12:52
  • The path is slightly different for VS 2010, it is in "Common7\IDE\ItemTemplates\CSharp\Code\1033" (notice additional Code folder). Also it probably makes sense to modify the Interface template in the same folder. – Massimiliano Jun 30 '10 at 12:33
  • For VS 2017 path is `"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs"` – tchelidze Apr 20 '17 at 11:45
  • 9
    Correct path for VS 2017 is `C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class` – Dmytro Laptin Jun 23 '17 at 15:35
  • I had all of the paths above and none of them worked. I finally found another visual studio directory for VS Pro. C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class – bwing May 15 '18 at 00:21
  • 3
    Defaulting to `internal` makes sense when developing a re-usable package, however it does not make sense in application code, especially when solution structure is divided into infrastructure/domain/application layers. In such cases most of the classes need to be accessible in other assemblies. – Laurynas Lazauskas Sep 15 '20 at 14:25
7

C# tends to default everything to the minimum scope necessary. This is a nice convention and quoted in Skeet's book (C# In Depth, p 224 "Note/Trivia"):

[Properties are the] only place where “private” is required—Everywhere else in C#, the default access modifier in any given situation is the most private one possible. In other words, if something can be declared to be private, then leaving out the access modifiers entirely will default it to being private. This is a nice element of language design, because it’s hard to get it wrong accidentally: if you want something to be more public than it is, you’ll notice when you try to use it.

Michael Haren
  • 105,752
  • 40
  • 168
  • 205
3

I prefer it the way it is. This way you have to consciously decide that you want it exposed to public. It's much like the argument, do you want your computer open to the outside world by default or would you rather configure it for internet access yourself.

Both approaches have their advantages, one has major potential security implications that I believe you should be aware of and make a conscious decision about rather than just it just happening automatically.

BenAlabaster
  • 39,070
  • 21
  • 110
  • 151
1

Because in C#, a class is internal by default. VisualStudio is therefore following the C# specification.

The following article explains access modifiers and default visibility in C#.

http://msdn.microsoft.com/en-us/library/ms173121.aspx

Winston Smith
  • 21,585
  • 10
  • 60
  • 75
1

Interestingly this only happens in C# - in vb.net you get a Public Class by default.

Personally, i prefer a public class by default as generally other classes need access to it. (most of my work is in the data layer though)

Pondidum
  • 11,457
  • 8
  • 50
  • 69
  • 1
    Do other classes outside your assembly really need access to most of your classes? I doubt it, but if so, you should reconsider how you group classes into assemblies! C# defaults to internal, where all classes in the same assembly have access. – Pontus Gagge May 05 '09 at 12:10
1

You could change the templates for a C# class - Usually located in "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#"

or even create your own template.

TWith2Sugars
  • 3,384
  • 2
  • 26
  • 43
  • sarnath'd - personally I like to be prompted with an actual keyword, but 99% of the time my classes are public anyway. Hmm, maybe that's a design smell... – annakata May 05 '09 at 12:07
1

Personally I prefer it as is, It forces you to actively think which classes you want to make public.

It ultimately defaults you into a hopefully cleaner API design and hence better more friendly software. You wouldn't want to expose the inner workings of your code, which would inevitably happen if everything defaulted to public.

This is somewhat subjective, personally I prefer everything to off and turn on only what I need not the other way round.

danswain
  • 4,171
  • 5
  • 37
  • 43
0

It is good practice in all areas of code to keep things as restricted as possible. Without specific reason for being otherwise, everything should be private, readonly (better yet const), and static. If a variable, method, or property can be private, make it private. If it can't be private, but can be protected, make it protected. If it can be readonly, make it readonly. If it can be static, make it static. The same is true for classes: they should be internal by default, and made public only when you have decided that this is a class you wish to export.

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80