Tuesday, 12 January 2010

Create a Code snippet in C#

What is snippet?

It is a reusability of set of codes for instance if we type ‘fo’ you will see following menu:


And if you press Tab and then again Tab the code block for ‘for’ loop appears like this:


More about snippets click here.

This is called code reusability or code snippet now at this post we are going to talk about how to make our own code snippet like this ‘for’ loop we are going to create our own code snippet for ‘for’ and will name it as ‘forloop’ let’s start:

Open a notepad file name it as ‘foorloop.snippet’ you can also add a XML file in Visual Studio 2008 and type these Tags.

Now copy the following code:

<CodeSnippets

xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<CodeSnippet Format="1.0.0">

<Header>

<Shortcut>

Shortcut>

<Title>

Title>

<Author>

Author>

Header>

<Snippet>

<Declarations>

<Literal>

<ID>ID>

<ToolTip>ToolTip>

<Default>Default>

Literal>

Declarations>

<Code Language="CSharp">

]]>

Code>

Snippet>

CodeSnippet>

CodeSnippets>

Now from start In Header we have ‘Shortcut’ where we define string what we should type to call this snippet, ‘Title’ is for Title of this snippet and ‘Author’ who is the Author of this snippet.

Now come to the snippet section inside snippet tag we have Declarations where we define the variables we ask the user to define it. Where ‘ID’ is the variable name for that definition ‘ToolTip’ is same as Tooltip and default is what should be shown there when this snippet is call. You can have more than one variable so copy the literal as many times.

Inside the Code section we define out set of codes now look at this finished example:

<CodeSnippets

xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

<CodeSnippet Format="1.0.0">

<Header>

<Shortcut>

forloop

Shortcut>

<Title>

For Loop

Title>

<Author>

Syed Hashmi

Author>

Header>

<Snippet>

<Declarations>

<Literal>

<ID>aID>

<ToolTip>Starting pointToolTip>

<Default>aDefault>

Literal>

<Literal>

<ID>bID>

<ToolTip>Length of the loopToolTip>

<Default>bDefault>

Literal>

Declarations>

<Code Language="CSharp">

for (int $a$ = 0; $a$ < $b$; $a$++)

{

}

]]>

Code>

Snippet>

CodeSnippet>

CodeSnippets>

Now here you can see we have two variables defined in ‘Declaration’ section one is ‘a’ and other is ‘b’ and we used them in code with ‘$a$’ this is the way we use variable in snippets.

After finishing this save it at any location and then import it from Snippet code manager from tool menu:


And now try typing ‘for’ and will notice ‘forloop’ added in this menu:


Press Tab and Tab and the code block of for loop is there:


This is post is only to show you the depth of snippet there are so many designers out which are easy to use to create own snippet like Visual Studio 2008 Code Snippet Library C# . Here are some Visual Studio 2008 Built-in Code Snippets.

Any suggestion or questions please comment. Thanks


1 comment:

Faisal Siddique said...

Thanks for this post. I found so many examples with VB but not with C# so easy to understand.