在VisualStudio中是否有快捷键可以创建方法,就像“prop, tab”用于属性和“ctor, tab”用于构造函数那样?
在VisualStudio中是否有快捷键可以创建方法,就像“prop, tab”用于属性和“ctor, tab”用于构造函数那样?
没有代码片段来创建除Main
之外的方法,但您可以执行以下操作。
输入您要创建的方法名称,传递参数,完成后,您会注意到方法名称开头有蓝色下划线。点击那个 (或者按下 Ctrl + .) ,这将为您提供创建方法的选项,例如:
private static void MySomeMethod(int a, string b)
{
throw new NotImplementedException();
}
另一个创建方法(提取)的巧妙方式如下。
如果我想把某个方法的一部分移动到新的私有方法中,我将使用这种方式。
这将只创建新的私有方法,但会自动设置输入参数和输出参数。
<?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>
这里是创建自定义代码片段的指南。
您可以制作自己的代码片段,或者使用此模板:
<?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>
在将此片段导入您的Visual Studio后,当您按下emptymethod
+ tab
时,您将获得新的未实现方法。
输入“fun”,然后按Tab键。 哒哒! 现在我需要输入更多字符,因为允许的最小字符数是30。