· c-sharp code-snippet Code standards Regions Snippet visualstudio VS

'Code Snippet – ‘Default’ regions in C# class'

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.

Expanded:

Collapsed:

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">
        <Header>
            <Title>Standard Class</Title>
            <Shortcut>stdcl</Shortcut>
            <Description>Create standard C# regions for this class.</Description>
            <Author>Nicklas Møller Jepsen</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
        <Declarations>
            <Literal default="true" Editable="false">
                    <ID>classname</ID>
                    <ToolTip>Class name</ToolTip>
                    <Function>ClassName()</Function>
                    <Default>ClassNamePlaceholder</Default>
                </Literal>

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

                #endregion

                #region Constructors

                public $classname$() { }

                #endregion

                #region Properties

                #endregion

                #region Methods

                #endregion
        $end$
        ]]>
        </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

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

 

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket