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

C#Visual Studio

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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

Solution 6 - C#

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)

Solution 7 - C#

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.

Solution 8 - C#

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.

Solution 9 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVadimView Question on Stackoverflow
Solution 1 - C#Anton GogolevView Answer on Stackoverflow
Solution 2 - C#Aaron HoffmanView Answer on Stackoverflow
Solution 3 - C#Michael HarenView Answer on Stackoverflow
Solution 4 - C#BenAlabasterView Answer on Stackoverflow
Solution 5 - C#Winston SmithView Answer on Stackoverflow
Solution 6 - C#PondidumView Answer on Stackoverflow
Solution 7 - C#TWith2SugarsView Answer on Stackoverflow
Solution 8 - C#danswainView Answer on Stackoverflow
Solution 9 - C#Dave CousineauView Answer on Stackoverflow