views:

259

answers:

6

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

A: 

I have heard complaints about this, I do not believe it an option that can be changed.

CSharpAtl
A: 

Do you mean the class itself is not available within other classes? Or do you mean that all of the contents of the file default to private access?

If you want the class to be visible in other classes, you just need to make sure that the namespace is visible in the other class (with the "using" keyword in c#, or just have them in the same namespace).

Don't know which language you are talking about, but in c# all methods, member variables, properties etc default to private. If you want them to be public you need to individually specify that for each member. I don't think it's possible to change this behaviour in c#.

marcush
A: 

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 :)

womp
+9  A: 

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

Shoban
Doh! you are too quick!
Chuck Conway
Agreed, that was QUICK!!!! Good answer.
Skittles
........ ;-) Thanks
Shoban
A: 

I don't think that you can change an option in VS so that all classes created are public by default (but I will look into it.)

I would think that unless that you have a very specific need for all your classes to be declared public (and I can't think of any) that you should leave the default as it is. This allows you to make decisions on exactly which classes are public and which should remain internal. In my opinion, a good design option would dictate that classes are declared 'internal' by default.

Skittles
A: 

A better solution than editing the existing templates, in my opionion, is to create your OWN templates that meet your requirements. Creating your own project and/or item templates let's you keep the default templates intact but get all the good stuff you need. For details on creating an item template check out my tip here: http://blogs.msdn.com/b/zainnab/archive/2010/08/17/roll-your-own-item-template-with-the-export-template-wizard-vstipproj0013.aspx

zainnab