简体   繁体   English

如何在Visual Studio中为getter和setter以及构造函数自动生成标准代码?

[英]How can I auto generate standard code for getters and setters and constructors in Visual Studio?

I created a class in C# with Visual Studio containing a bunch of variables. 我在C#中用Visual Studio创建了一个包含一堆变量的类。 How can I auto-generate Constructors, getters and setters for them? 如何为它们自动生成构造函数,getter和setter? I know such features from other programming software for developing in Java like Android Studio and Eclipse and I don't find this feature in Visual Studio. 我知道其他编程软件的这些功能,用于开发Java,如Android Studio和Eclipse,我在Visual Studio中找不到这个功能。

Auto Implemented Properties 自动实现的属性

This: 这个:

public string TestString { get; set; }

Is equivalent to this: 相当于:

private string _testString;

public string TestString
{
    get
    {
        return _testString;
    }
    set
    {
        _testString = value;
    }
}

Code snippets 代码片段

If you are using Visual Studio, you can use code snippets to generate boilerplate code for you. 如果您使用的是Visual Studio,则可以使用代码段为您生成样板代码。

Type ctor and press Tab . 键入ctor并按Tab键 It will generate a parameterless constructor for the class you are currently in. 它将为您当前所在的类生成无参数构造函数。

Type prop and press Tab . 键入prop并按Tab键 Then type the datatype you want your property to be and press Tab twice. 然后键入您希望属性的数据类型,并按两次Tab键 Finally, type the name of the property and press Tab one last time. 最后,键入属性的名称,最后按Tab键一次。 It will generate an auto implemented property for you. 它将为您生成一个自动实现的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何在 Visual Studio 中生成 getter 和 setter? - How can we generate getters and setters in Visual Studio? 在Visual Studio中生成Getter和Setter的简便方法 - Easy way to generate Getters and Setters in Visual Studio 如何使Visual Studio CTRL-KD不展开getter和setter - How can I get Visual Studio CTRL-K-D to not unroll getters and setters 如何将getter和setter参数传递给连接类? - How can I pass my getters and setters parameters to connection class? getter和setter中的冗余代码 - Redundant code in getters and setters 这个代码有效吗? 我可以在 class 中拥有一个没有公共 getter 和 setter 的私有字段吗? - Is this code valid? Can I have a private field in a class without public getters and setters? 如何从Visual Studio 2012中编写的C#代码生成Visio 2010中的UML图? - How can I generate UML diagrams from C# code written in Visual Studio 2012 into Visio 2010? 如何在Visual Studio中保存时从文本文件生成普通的旧代码? - How can I generate plain old code from a text file on save with visual studio? 如何在Visual Studio中创建加载项以从设置文件生成javascript,CSS和其他代码 - How can I create an addin in Visual Studio to generate javascript, CSS and other code from a settings file 您可以自动生成用于在C#中分配对象属性的代码吗? (视觉工作室) - Can you auto-generate code for assigning properties of an object in C#? (Visual Studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM