Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my classes private by default in Visual Studio?

WIen I create a new class file, Visual Studio does not make it public by default. Can I change this?

like image 724
mrblah Avatar asked May 27 '09 03:05

mrblah


2 Answers

By default a class without an access specifier is internal and a member defaults to private. This it keeps the visibility as restricted as possible and thereby increases encapsulation.

Making a new class public without even thinking about breaks the entire idea of encapsulation.

The class template that VS uses to create a new class can be found in this zip file (in case of CSharp):

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip or C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033 when using VS Orcas

Open the zip file, add the public keyword, save it and you're done. Every time you add a class it will be public. Hope this helps.

Source

like image 119
Shoban Avatar answered Sep 29 '22 16:09

Shoban


There are other issues with the default code templates - for example, they are not compliant with StyleCop out of the box, since the "using" lines are outside the namespace declaration.

You could use ReSharper to create your class files from templates, allowing you to put any code you want into the template. That's what I do :)

like image 37
womp Avatar answered Sep 29 '22 18:09

womp