27

Is there a shortcut in VisualStudio to create a method, like there is "prop, tab" for a property and "ctor, tab" for a constructor?

Jo Smo
  • 6,923
  • 9
  • 47
  • 67

6 Answers6

25

There is no Code snippet to create a method other than Main, but you can do the following.

Type your to be method name, pass the parameters, Once done you will notice a blue under line at the beginning of method name. Click that (or click Ctrl + . ) that will give you the option to create method like:

enter image description here

This will generate a method like:

private static void MySomeMethod(int a, string b)
{
    throw new NotImplementedException();
}
general03
  • 855
  • 1
  • 10
  • 33
Habib
  • 219,104
  • 29
  • 407
  • 436
13

There is another clever way for create method (extract).

This way I use if I have method and I would like part of this method move to new private method.

  1. Select part of code in method which you would like to extract.
  2. Press Ctrl + R + M or right click on selected code → Refactor\Extract\Extract Method...

This will create only new private method but automatically set input parameters and output parameter.

Dušan Kalivoda
  • 179
  • 1
  • 7
9
  1. Save the following Code snippet into a file with '.snippet' extension
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Generate Method Stub</Title>
          <Description>Create a new method</Description>
          <Author>Anoop Simon</Author>
         <Shortcut>stub</Shortcut>
        </Header>
        <Snippet>
           <Code Language="CSharp">
          <![CDATA[public string DummyMethod(string arg1,string arg2)
          {
              return string.Empty;
          }
          ]]>
      </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
  1. Open Visual Studio .
  2. Got to Tools --> Code Snippets Manager.. (Ctrl +K , Ctrl + B) enter image description here

  3. Import the file saved earlier

  4. Click OK
  5. Open Any C# class in Visual Studio IDE
  6. type 'stub' then press TAB key twice . If you wish to change the shortcut, update value of tag in the snippet file
Anoop
  • 146
  • 1
  • 6
8

check Code Snippets

sim: static int main method

svm: static void main method

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

Here is a guidelines to create custom code snippet.

You can make your own code snippet, or just use this template:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>create new 'not implemented' method</Title>
      <Description>create new 'not implemented' method</Description>
      <Shortcut>emptymethod</Shortcut>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[        $access specifier$ $return type$ $methodName$($params$)
        {
            $throw new NotImplementedException()$;
        }]]>
      </Code>
      <Declarations>
        <Literal>
          <ID>access specifier</ID>
          <Default>Array</Default>
        </Literal>
        <Literal>
          <ID>return type</ID>
          <Default>returnType</Default>
        </Literal>
        <Literal>
          <ID>methodName</ID>
          <Default>methodName</Default>
        </Literal>
        <Literal>
          <ID>params</ID>
          <Default>params</Default>
        </Literal>
        <Literal>
          <ID>throw new NotImplementedException()</ID>
          <Default>throw new NotImplementedException()</Default>
        </Literal>
      </Declarations>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

after you import this snippet to your visual studio, when you press emptymethod + tab , you will get new not implemented method.

Niro
  • 43
  • 3
-2

Type 'fun' then tab. Ta-da! And now I need to type more characters because the minimum allowed number of characters is 30.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 14 '22 at 02:51