Is there a shortcut in VisualStudio to create a method?

南楼画角 提交于 2020-06-10 14:52:06

问题


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


回答1:


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();
}



回答2:


check Code Snippets

sim: static int main method

svm: static void main method




回答3:


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.




回答4:


  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)

  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


来源:https://stackoverflow.com/questions/23811413/is-there-a-shortcut-in-visualstudio-to-create-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!