Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 666
Vadim Avatar asked May 05 '09 12:05

Vadim


People also ask

Is C# class public by default?

Classes are internal by default. Class members, including nested classes, can be public, protected internal, protected, internal, private, or private protected. Members are private by default.

What is difference between internal and public in C#?

internal is useful when you want to declare a member or type inside a DLL, not outside that. Normally, when you declare a member as public , you can access that from other DLLs. But, if you need to declare something to be public just inside your class library, you can declare it as internal .

How do I create a new class in Visual Studio?

To add a class in a Visual Studio C++ project, in Solution Explorer, right-click the project, choose Add, and then choose Class. This command opens the Add Class dialog box. When you add a class, you must specify a name that is different from classes that already exist in MFC or ATL.

How do I change a class template in Visual Studio?

Choose File > New > Project from the menu bar. Select the template that you want to update and continue through the steps to create the new project. Modify the project in Visual Studio. For example, change the output type or add a new file to the project.


1 Answers

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.

like image 173
Anton Gogolev Avatar answered Sep 16 '22 11:09

Anton Gogolev