Edward Tanguay
Edward Tanguay

Reputation: 193302

Code snippets for methods in Visual Studio

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?

Upvotes: 71

Views: 76929

Answers (8)

ANewGuyInTown
ANewGuyInTown

Reputation: 6437

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

  • Press Ctrl + K and then Ctrl + X

Upvotes: 17

Scott Nimrod
Scott Nimrod

Reputation: 11595

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)

Upvotes: 43

UJS
UJS

Reputation: 861

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.

Upvotes: 9

Kevin Hogg
Kevin Hogg

Reputation: 1781

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()
{

}

Upvotes: 13

Cameron MacFarland
Cameron MacFarland

Reputation: 71856

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

Upvotes: 123

Soraz
Soraz

Reputation: 6746

The code snippet for properties is:

propTABTAB

Upvotes: -2

daniel1426
daniel1426

Reputation: 169

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>

Upvotes: 1

EdgarT
EdgarT

Reputation: 1088

You can create customs snippets. Like this:

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

Upvotes: 0

Related Questions