Tuesday, December 29, 2009

New Class Templates in C#

My team finally upgraded to .NET 3.5 along with Visual Studio 2008 recently, which has been a huge source of happiness for me. However, since the upgrade, I've been getting annoyed with VS's insistence that I always include the Linq library in each of my new classes. I typically remove all of the default using directives anyway, but since I don't automatically reference the System assembly that contains the Linq definition, I was getting a pre-compile error from R# every time I added a new item to a project. In addition, I've been growing tired of having to type "public" each time I add a new class (since the default is nothing). So, I decided to take action.

A little bit of web searching lead me to the Visual Studio Template Reference. Each time a new item is created in Visual Studio, VS finds the template definition that matches the item and generates the code to match. Templates support logical control flow and variable replacement. By default the C# class templates are stored in "\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033", with similar templates stored nearby. The instructions are geared the reader toward creating their own templates for new types of items, which is a very useful tool (but I can do that with resharper), but I really just wanted to change the default. So I went in and did it: no more "using" and always public.

Before my changes would take effect, I first had to close visual studio and then run the following command,

devenv /installvstemplates

which will rebuild the VS templates run-time cache folder.

Now I can focus on all the fun features of 3.5!

1 comment:

  1. hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I

    was with the information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful

    for .Net community.

    There is a good C# resource site, Have alook