问题
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:
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.
- 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.
回答4:
- 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>
- Open Visual Studio .
Got to Tools --> Code Snippets Manager.. (Ctrl +K , Ctrl + B)
Import the file saved earlier
- Click OK
- Open Any C# class in Visual Studio IDE
- 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