Jo Smo
Jo Smo

Reputation: 7169

Is there a shortcut in VisualStudio to create a method?

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

Upvotes: 28

Views: 59241

Answers (6)

Habib
Habib

Reputation: 223247

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 red underline at the beginning of method name. Click that (or click Ctrl + .), it will give you the option to create method like this:

Screenshot of implementation

This will generate a method like the following:

private static void MySomeMethod(int a, string b)
{
    throw new NotImplementedException();
}

Upvotes: 25

Niro
Niro

Reputation: 43

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.

Upvotes: 0

Markus Botha
Markus Botha

Reputation: 7

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

Upvotes: -3

Anoop
Anoop

Reputation: 156

  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

Upvotes: 10

Dušan Kalivoda
Dušan Kalivoda

Reputation: 189

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.

Upvotes: 14

Sajeetharan
Sajeetharan

Reputation: 222582

check Code Snippets

sim: static int main method

svm: static void main method

Upvotes: 8

Related Questions