Example of Code Snippet for Visual Studio

Ever wondered how to write code snippet? Have a look at these simple example code snippets – these are specially for people who learn by looking!

You have to place these code snippets (basically XML) into file with .snippet extension. Then you can put all your snippet files into appropriate directory. This directory can be loaded into VS from Tools>Code Snippets Manager>Add. Enjoy!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://NewtonicaOnline.com/VS/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Copy Right - crc</Title>
            <Description>Adds copyright comment</Description>
            <Author>Chetan Panchal</Author>
            <HelpUrl>http://www.NewtonicaOnline.com</HelpUrl>
            <Shortcut >crc</Shortcut>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
            </Header>
            <Snippet>
                <Declarations>
                    <Literal>
                        <ID>Descr</ID>
                        <ToolTip>Replace with brief description of file content.</ToolTip>
                        <Default>Brief description</Default>
                    </Literal>
                </Declarations>
                <Code Language="CSharp" Kind="any">
                     <![CDATA[// You can distribute or derive from this work without removing following lines.
                     // (C) Copyright 2008, Chetan Panchal, All rights reserved, http://www.NewtonicaOnline.com
                     // Description: $Descr$$end$]]>
                </Code>
            </Snippet>
    </CodeSnippet>
    <CodeSnippet Format="1.0.0">
      <Header>
                <Title>Console.WriteLine Surrounds</Title>
                <Description>Surrounds selection with Console.WriteLine</Description>
                <Author>Chetan Panchal</Author>
                <HelpUrl>http://www.NewtonicaOnline.com</HelpUrl>
                <Shortcut >cws</Shortcut>
                <SnippetTypes>
                     <SnippetType>SurroundsWith</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
                <Declarations>
                    <Literal Editable="false">
                        <ID>SystemConsole</ID>
                        <Function>SimpleTypeName(global::System.Console)</Function>
                    </Literal>
                </Declarations>
                <Code Language="CSharp" Kind="method body">
                     <![CDATA[$SystemConsole$.WriteLine($selected$$end$);]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    <CodeSnippet Format="1.0.0">
       <Header>
          <Title>Console.ReadLine</Title>
          <Description>Inserts Console.ReadLine()</Description>
          <Author>Chetan Panchal</Author>
          <HelpUrl>http://www.NewtonicaOnline.com</HelpUrl>
          <Shortcut >cr</Shortcut>
          <SnippetTypes>
             <SnippetType>Expansion</SnippetType>
          </SnippetTypes>
       </Header>
       <Snippet>
          <Declarations>
            <Literal Editable="false">
              <ID>SystemConsole</ID>
                            <Function>SimpleTypeName(global::System.Console)</Function>
                        </Literal>
          </Declarations>
          <Code Language="CSharp" Kind="method body">
             <![CDATA[$SystemConsole$.ReadLine();$end$]]>
          </Code>
       </Snippet>
    </CodeSnippet>
 </CodeSnippets>
  • RSS
  • Print
  • PDF
  • Twitter
  • del.icio.us
  • Facebook
  • LinkedIn
  • Google Bookmarks
  • Digg
  • Add to favorites
  • StumbleUpon

Post a Comment

Your email is never published nor shared. Required fields are marked *