70

In Visual Studio I can type e.g.

for TAB TAB

and a code snippet pops in.

Are there built-in code snippets for private, public, etc. methods as well?

8 Answers 8

123

ctor: Default constructor

prop: Property

propg: Read-only property

sim: static int main method

svm: static void main method

There's a good list here. And if you want to make your own, the Snippet Designer is very good.

Here are all the Visual C# code snippets for Visual Studio 2017

6
  • You insert it with a shortcut keyword, like meth<Tab><Tab>, by going into the Properties pane and setting the Shortcut option (e.g., "meth")?
    – bambams
    Sep 19, 2011 at 17:10
  • The link to the list gives a 404 Jan 25, 2013 at 18:58
  • 1
    @FelixMM Odd it redirected for me. Anyway I've updated it to the new location. Jan 25, 2013 at 23:38
  • @CameronMacFarland it's awesome. please update it with 2017 link :) Apr 12, 2017 at 8:06
  • Is there no built in snippet that allows you to do something like sim or svm and allow you to tab through it and modify it?
    – Samir
    Jun 23, 2017 at 9:29
43

You can download the method snippets as a Visual Studio Extension.

It supports the following:

method (typical method)

vmethod (virtual method)

smethod (static method)

xmethod (extension method)
  1. In Visual Studio, go to menu ToolsExtensions and Updates...

  2. Observe the Extensions and Updates window

  3. Enter "C# Methods Code Snippets" in the search field (upper right)

3
  • 1
    This is the only answer that actually answers the question - code snippets for methods (from the question: "...code snippets for private, public, etc. methods "). The other answers are just generic answers about snippets. Jul 25, 2019 at 11:58
  • I tried the search in Visual Studio 2012 (yes, I know) without finding the extension. I selected "Online" before entering "C# Methods Code Snippets". I looked at the first 20 pages of 25 hits each (no end in sight). But I did find a number interesting items: "Import Code Snippet", "CodeHelperTools", "NUnit VS Templates", "StyleCop", "Language Convert" (C# <-> VB.NET), "Script#" (transpile from C# to JavaScript), Jul 25, 2019 at 12:57
  • cont' - "Refactor" (.NET assembly browser and decompiler), "kiss!" (template-based code generator for C#, VB.NET, and SQL), "WPF MVVM project template", "T4Awesome", "SharpSetup" (WiX, create installers), "Units of Measurements", ".NET FTP library", and "Comment". Jul 25, 2019 at 12:59
17

If you want to see the list of all available snippets:

  • Press Ctrl + K and then Ctrl + X
3
  • Just to clarify, that's CTRL + K and then CTRL + X. For more info, see How to: Use Code Snippets (C#)
    – Samir
    Jun 23, 2017 at 9:52
  • @Samir. Actually both works. However, updated to what Microsoft doc says. Feb 18, 2019 at 0:37
  • That's right. It depends on how you formulate it. What was missing was "press and hold CTRL". But this works too.
    – Samir
    Feb 26, 2019 at 22:41
13

Below are the steps I used to create a custom snippet for Visual Studio 2010, but the steps work for Visual Studio 2008.

Create a new text file named method.snippet and paste the following:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>method</Title>
            <Shortcut>method</Shortcut>
            <Description>Code snippet for method</Description>
            <Author>Kevin Hogg</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>methodname</ID>
                    <ToolTip>Method name</ToolTip>
                    <Function>MethodName()</Function>
                    <Default>MethodNamePlaceholder</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[public void $methodname$ ()
    {
        $end$
    }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Copy your file into the snippets folder in Windows Explorer:

  • Visual Studio 2010: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#
  • Visual Studio 2008: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#

Once you save your file, the snippets are automatically loaded, so you can now open Visual Studio and type:

method<tab><tab>

*where <tab> is the Tab key on your keyboard.

You should now see the following created, with the MethodNamePlaceholder highlighted so you can change the name.

public void MethodNamePlaceholder()
{

}
4
  • Please note I could only test this in Visual Studio 2010
    – Kevin Hogg
    Oct 8, 2013 at 12:33
  • 2
    It also works on VS2013 Path : C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\1033\Visual C#
    – zapoo
    Aug 4, 2015 at 14:21
  • 1
    Works VS2015. Thanks. Great DYI. Check the box for learned something new today.
    – Rourke
    Sep 22, 2017 at 13:38
  • 1
    VS2019: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC#\Snippets\1031\Visual C#
    – nilsK
    Jun 4, 2020 at 13:33
9

Some of the snippets I use, also mentioned at MSDN, follows:

  1. '#if Creates a #if directive and a #endif directive.
  2. '#region Creates a #region directive and a #endregion directive.
  3. ~ Creates a destructor for the containing class.
  4. attribute Creates a declaration for a class that derives from Attribute.
  5. checked Creates a checked block.
  6. class Creates a class declaration.
  7. ctor Creates a constructor for the containing class.
  8. cw Creates a call to WriteLine.
  9. do Creates a do while loop.
  10. else Creates an else block.
  11. enum Creates an enum declaration.
  12. equals Creates a method declaration that overrides the Equals method defined in the Object class.
  13. exception Creates a declaration for a class that derives from an exception (Exception by default).
  14. for Creates a for loop.
  15. foreach Creates a foreach loop.
  16. forr Creates a for loop that decrements the loop variable after each iteration.
  17. if Creates an if block.
  18. indexer Creates an indexer declaration.
  19. interface Creates an interface declaration.
  20. invoke Creates a block that safely invokes an event.
  21. iterator Creates an iterator.
  22. iterindex Creates a "named" iterator and indexer pair by using a nested class.
  23. lock Creates a lock block.
  24. mbox Creates a call to MessageBox.Show. You may have to add a reference to System.Windows.Forms.dll.
  25. namespace Creates a namespace declaration.
  26. prop Creates an auto-implemented property declaration.
  27. propfull Creates a property declaration with get and set accessors.
  28. propg Creates a read-only auto-implemented property with a private "set" accessor.
  29. sim Creates a static int Main method declaration.
  30. struct Creates a struct declaration.
  31. svm Creates a static void Main method declaration.
  32. switch Creates a switch block.
  33. try Creates a try-catch block.
  34. tryf Creates a try-finally block.
  35. unchecked Creates an unchecked block.
  36. unsafe Creates an unsafe block.
  37. using Creates a using directive.
  38. while Creates a while loop.
1
  • They can be discovered by Ctrl + K, Ctrl + X (at least with some versions of Visual Studio, including Visual Studio 2012). Perhaps include it in the answer? Sep 4, 2020 at 23:15
1

I made my own snippet for a method. The XML code for it is the following, and you can add it to a file called "my_method.snippet" (or whatever_you_want.snippet) in C:\Users\YOUR_USERNAME\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets (your path might be different because I use VS2012):

<CodeSnippet Format="1.0.0">
    <Header>
        <Title>method</Title>
        <Shortcut>method</Shortcut>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>access_modifier</ID>
                <Default>private</Default>
            </Literal>
            <Literal>
                <ID>return_type</ID>
                <Default>void</Default>
            </Literal>
            <Literal>
                <ID>name</ID>
                <Default>New_method</Default>
            </Literal>
        </Declarations>
        <Code Language="csharp">
            <![CDATA[$access_modifier$ $return_type$ $name$ ()
    {
    $end$
    }]]>
        </Code>
    </Snippet>
</CodeSnippet>
0

You can create customs snippets. Like this:

http://www.mediafire.com/file/gz3tzjnydk5/meth.snippet

1
  • That link is for a page about the meth.snippet XML document format (.SNIPP). That is far away from being about custom snippets. I suggest adding an example and/or more information to make this answer more useful. Jul 25, 2019 at 11:35
-2

The code snippet for properties is:

propTABTAB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.