Code Snippet: Default regions in C# class

By: Nicklas Møller Jepsen

I’m currently trying to standardizes the way I’m writing code. To achieve this I simply try to comply to some simple code standards. This is, in some areas, easy to accomplish because of the Visual Studio auto format option.

Why use Regions in C

The auto format of source code, however, do not take care off where in the file the different parts that together becomes a class is placed. Therefore I decided to use regions. Regions are good at many things for instance you can collapse/expand your regions (see below) and you can group methods/properties/fields and so forth accordingly.



The C# Code Snippet

Well, now to the Code Snippet as I’ve created based on this StackOverflow post.

The snippet creates this layout of the class:

Of course a class can contain a lot more then this, but I think as a general purpose snippet, this will do.

The XML of the snippet is here:

<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
			<Title>Standard Class</Title>
			<Description>Create standard C# regions for this class.</Description>
			<Author>Nicklas Møller Jepsen</Author>
			<Literal default="true" Editable="false">
                    <ToolTip>Class name</ToolTip>

		<Code Language="csharp" Kind="method head"><![CDATA[#region Fields


				#region Constructors

				public $classname$() { }


				#region Properties


				#region Methods


You can download the snippet here.

Please let me know if you have any comments to the snippet or let me know if you have any great ideas for some new snippets :)


Cheers, Nicklas