使用变量创建构造函数的快捷方式(C# Visual Studio 2010)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8893979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shortcut for creating constructor with variables (C# Visual Studio 2010)
提问by Markus
In Visual Studio 2010 C# you can, in a class, type ctorand then press taband Visual Studio will create a constructor for that class for me. It is very convenient.
在 Visual Studio 2010 C# 中,您可以在类中键入ctor然后按tab,Visual Studio 将为我创建该类的构造函数。非常方便。
But is there a way to make Visual Studio create a constructor with all my variables, properties and so on?
但是有没有办法让 Visual Studio 创建一个包含我所有变量、属性等的构造函数?
For example,
例如,
public class User
{
public String UserName { get; private set; }
}
And for this I want ctor+ tabto make me a
为此,我希望ctor+tab让我成为
public User(string UserName)
{
this.UserName = UserName;
}
采纳答案by Markus
Thanks to Samuel Slade (telling me it's called code-snippets) I managed to find another Stack Overflow answer: Snippet code to create constructor in VS2010 Express
感谢 Samuel Slade(告诉我它被称为代码片段)我设法找到了另一个 Stack Overflow 答案:Snippet code to create constructor in VS2010 Express
And it seems as the answer is NO, not without a plugin/extension.
似乎答案是否定的,不是没有插件/扩展。
Many refer to the ReSharperextension.
许多人指的是ReSharper扩展。
回答by Samuel Slade
I think what you are referring to is Code Snippets. You can write your own Code Snippets (they are written in XML). See herefor a starting point.
我认为您所指的是代码片段。您可以编写自己的代码片段(它们是用 XML 编写的)。请参阅此处了解起点。
You should also be able to edit existing Code Snippets (such as the ctorone). Refer to MSDNfor some direction on this.
您还应该能够编辑现有的代码片段(例如那个ctor)。请参阅MSDN以获得有关此的一些方向。
Note: Further Googling on Code Snippets will bring up more tutorials and references.
注意:进一步谷歌搜索代码片段将带来更多教程和参考。
回答by Andreas Rohde
I think you could do this with a snippet:
我想你可以用一个片段来做到这一点:
See Creating and Using IntelliSense Code Snippets(MSDN)
请参阅创建和使用 IntelliSense 代码片段(MSDN)
回答by Marc Gravell
You can sort of do this the other way around; if you start withoutthe constructor or field, and try to use the non-existent constructor, you can press ctrl+.to ask it to generate one for you, usage-first:
你可以反过来做;如果您开始时没有构造函数或字段,并尝试使用不存在的构造函数,您可以按ctrl+.要求它为您生成一个,使用优先:


This compiler then generates something not too dissimilar:
这个编译器然后生成一些不太相似的东西:
public class User
{
private string username;
public User(string username)
{
// TODO: Complete member initialization
this.username = username;
}
}
You can then fix this up manually if needed (perhaps using the inbuilt rename refactor, etc). But not quitewhat you wanted.
然后,您可以根据需要手动修复此问题(可能使用内置的重命名重构等)。但并不完全是你想要的。
回答by LTR
回答by Thiago Burgos
The "ctor" code snippet only creates a blank constructor, but does not use the existing attributes of the class in this constructor.
“ctor”代码片段仅创建一个空白构造函数,但不使用该构造函数中类的现有属性。
However, the latest versions of ReSharperenables you to choose the fields to be included in a constructor (like Eclipse does since a long time ago).
但是,最新版本的ReSharper允许您选择要包含在构造函数中的字段(就像很久以前 Eclipse 所做的那样)。
回答by IndieTech Solutions
回答by dirtyw0lf
Use ReSharper'sctorf.
使用ReSharper 的ctorf。
This will allow you to create a constructor with generated arguments based on fields defined in the class.
这将允许您使用基于类中定义的字段生成的参数创建构造函数。

