Today it frustrated me again that every time I add a new class in visual studio 2005 (or Orcas) I have to add the 'public' access modifier to the class I created. I decided to do something about it.
The class template that VS uses to create a new class can be found in this zip file (in case of CSharp):
C:Program FilesMicrosoft Visual Studio 8Common7IDEItemTemplatesCSharp1033Class.zip or
C:Program FilesMicrosoft Visual Studio 9.0Common7IDEItemTemplatesCSharpCode1033 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.
Of course you can make other changes you like and there are also other zip files with templates in it. I think the names speak for themselves.
7 comments
Better yet, you can save your own class files and projects as templates!
With the C# IDE extensions in VS 2005 enabled you will have a ‘Save as Template’ function in the file menu. This will create a zipfile which you can deploy to all the dev-stations in the project.
bartg
Why would you want the default to be public? The defaults were not chosen at random!! They force you to think twice before increasing the visibility.
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.
frankb
I am with Frank on this one. I love having most classs internal, by separating code in projects you really need to think about what gets to be public
Wouter
To level out the discussion: I’d prefer not using the default as things should be explicit and I agree with Frank: things should be as hidden as possible.
ernow
So my tendency to remove the private keyword for methods and fields will probably not suite you.. ? 🙂
I am a minimalist (also called lazy in some countries)
wouterv
In programming lazyness is a generally a good thing. As stated before; the defaults were not choosen at random. These are the options you usually want unless you explictly choose something else.
When I start Word it automaticaly opens a default template (Letter.dot) because that is the one I use most of the time. I don’t want to explicitly choose that template every time!
frankb
I agree with you guys that one should keep the visibility as restricted as possible. But too often I have to modify the modifier. This is probably due to the kind of software we create for Endeavour. A great part of the software is framework stuff and keeping most things internal doesn´t make a framework very usable 🙂 Every time I create a class I still ask myself what visibility it should have up front.
Patrick Gilhuijs