I write a lot of C# code, so anything I can find to reduce the amount of time I spend “filling in the blanks” is great. Visual Studio is perfect for helping out in these types of situations. A case-in-point is the custom item template.
Whenever you select “Add New Item” on a project, you are shown a list of templates for the different items you can create. These items range from new classes to database files to xml files. I have found over time that I tend to always create the same basic structure for every class I write in C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProjectName
{
/// <summary>
/// ClassName
/// </summary>
public class ClassName
{
#region Private Data Members
#endregion
#region Public Accessors
#endregion
#region Constructors
/// <summary>
/// Construct a default instance of ClassName
/// </summary>
public ClassName
{
}
#endregion
}
}
After filling out the same basic structure a number of times, I realized it was time to automate. Fortunately, as I mentioned, VS08 makes this ridiculously easy. First create a file to use as your template, then under the “File” menu, select the “Export Template…” option.
A dialog will appear asking if you are creating an item template or a project template. In this case, we are creating an item, so select the item option, and choose the project containing the file you created to use as a template. Next, you will select the actual file from the project. Note that you can select multiple files if your template needs supporting files. Select the references to include, and then provide a name and description for the file. make sure you have it automatically import the template, and you are finished.
If you are curious about the template structure, you view the template files as a zipped file under “Documents\Visual Studio 2008\My Exported Templates”. By editing the files in the zip file you can further refine and enhance your template, and you can fix any problems that might have occurred during export.