Is there a shortcut in VisualStudio to create a method, like there is "prop, tab" for a property and "ctor, tab" for a constructor?
-
1possible duplicate of [Visual Studio code snippet for method?](http://stackoverflow.com/questions/292164/visual-studio-code-snippet-for-method) – Farhad Jabiyev May 22 '14 at 15:43
-
You are welcome. But Habib's answer is also useful. – Farhad Jabiyev May 22 '14 at 15:48
-
Try resharper. It does everything good in VS. – Aron May 22 '14 at 15:52
-
) In my opinion Habib's answer is more appropriate. I was late about some seconds to post the same answer. )) – Farhad Jabiyev May 22 '14 at 15:58
-
@FarhadJabiyev yeah...it might be. I just don't know what VS can do since 2008, since I always run Resharper. – Aron May 22 '14 at 16:08
6 Answers
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:
This will generate a method like:
private static void MySomeMethod(int a, string b)
{
throw new NotImplementedException();
}
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.
- Select part of code in method which you would like to extract.
- 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.

- 179
- 1
- 7
- 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>

- 146
- 1
- 6
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.

- 43
- 3
Type 'fun' then tab. Ta-da! And now I need to type more characters because the minimum allowed number of characters is 30.
-
1As 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