C# 我们如何在 Visual Studio 中生成 getter 和 setter?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3017/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 13:33:50  来源:igfitidea点击:

How can we generate getters and setters in Visual Studio?

提问by Paul

By "generate", I mean auto-generation of the code necessary for a particular selected (set of) variable(s).

通过“生成”,我的意思是自动生成特定选定(一组)变量所需的代码。

But any more explicit explication or comment on good practice is welcome.

但欢迎对良好实践进行更明确的解释或评论。

采纳答案by Orion Edwards

Rather than using Ctrl+ K, Xyou can also just type propand then hit Tabtwice.

除了使用Ctrl+ KX您还可以只输入prop然后点击Tab两次。

回答by Mario Marinato

I don't have Visual Studio installed on my machine anymore (and I'm using Linux), but I do remember that there was an wizard hidden somewhere inside one of the menus that gave access to a class builder.

我的机器上不再安装 Visual Studio(我使用的是 Linux),但我确实记得有一个向导隐藏在其中一个菜单中,可以访问类生成器。

With this wizard, you could define all your classes' details, including methods and attributes. If I remember well, there was an option through which you could ask Visual Studio to create the setters and getters automatically for you.

使用此向导,您可以定义所有类的详细信息,包括方法和属性。如果我没记错的话,有一个选项可以让 Visual Studio 自动为你创建 setter 和 getter。

I know it's quite vague, but check it out and you might find it.

我知道它很模糊,但检查一下,你可能会找到它。

回答by Jon Limjap

By generate, do you mean auto-generate? If that's not what you mean:

通过生成,您的意思是自动生成吗?如果这不是你的意思:

Visual Studio 2008 has the easiest implementation for this:

Visual Studio 2008 对此有最简单的实现:

public PropertyType PropertyName { get; set; }

In the background this creates an implied instance variable to which your property is stored and retrieved.

在后台,这会创建一个隐含的实例变量,您的属性存储和检索到该变量。

However if you want to put in more logic in your Properties, you will have to have an instance variable for it:

然而,如果你想在你的属性中加入更多的逻辑,你必须有一个实例变量:

private PropertyType _property;

public PropertyType PropertyName
{
    get
    {
        //logic here 
        return _property;
    }
    set
    {
        //logic here
        _property = value;
    }
 }

Previous versions of Visual Studio always used this longhand method as well.

以前版本的 Visual Studio 也总是使用这种普通方法。

回答by omar

If you are using Visual Studio 2005 and up, you can create a setter/getter real fast using the insert snippet command.

如果您使用的是 Visual Studio 2005 及更高版本,则可以使用插入片段命令快速创建 setter/getter。

Right click on your code, click on Insert Snippet(Ctrl+K,X), and then choose "prop" from the list.

右键单击您的代码,单击Insert Snippet( Ctrl+ K, X),然后从列表中选择“prop”。

回答by Dan Herbert

Visual Studio also has a feature that will generate a Property from a private variable.

Visual Studio 还有一项功能,可以从私有变量生成属性。

If you right-click on a variable, in the context menu that pops up, click on the "Refactor" item, and then choose Encapsulate Field.... This will create a getter/setter property for a variable.

如果右键单击一个变量,在弹出的上下文菜单中,单击“Refactor”项,然后选择Encapsulate Field...。这将为变量创建一个 getter/setter 属性。

I'm not too big a fan of this technique as it is a little bit awkward to use if you have to create a lot of getters/setters, and it puts the property directly below the private field, which bugs me, because I usually have all of my private fields grouped together, and this Visual Studio feature breaks my class' formatting.

我不太喜欢这种技术,因为如果您必须创建很多 getter/setter,使用它会有点尴尬,并且它将属性直接放在私有字段下方,这让我很烦恼,因为我通常将我所有的私有字段组合在一起,这个 Visual Studio 功能打破了我班级的格式。

回答by Daniel

In addition to the 'prop' snippet and auto-properties, there is a refactor option to let you select an existing field and expose it via a property (right click on the field → RefactorEncapsulate Field...).

除了“prop”片段和自动属性之外,还有一个重构选项,让您可以选择现有字段并通过属性公开它(右键单击该字段 →重构封装字段...)。

Also, if you don't like the 'prop' implementation, you can create your own snippets. Additionally, a third-party refactoring tool like ReSharperwill give you even more features and make it easier to create more advanced snippets. I'd recommend ReSharper if you can afford it.

此外,如果您不喜欢 'prop' 实现,您可以创建自己的代码段。此外,像ReSharper这样的第三方重构工具将为您提供更多功能,并使创建更高级的片段变得更加容易。如果您负担得起,我会推荐 ReSharper。

回答by Oliver

If you're using ReSharper, go into the ReSharper menu → CodeGenerate...

如果您使用的是 ReSharper,请进入 ReSharper 菜单 →代码生成...

(Or hit Alt+ Insinside the surrounding class), and you'll get all the options for generating getters and/or setters you can think of :-)

(或在周围的类中点击Alt+ Ins),您将获得生成您能想到的 getter 和/或 setter 的所有选项:-)

回答by Chirag Khatsuriya

You can also use "propfull" and hit TABtwice.

您也可以使用“propfull”并点击TAB两次。

The variable and property with get and set will be generated.

将生成带有 get 和 set 的变量和属性。

回答by avinash kadu

Use the propfullkeyword.

使用propfull关键字。

It will generate a property and a variable.

它将生成一个属性和一个变量。

Type keyword propfullin the editor, followed by two TABs. It will generate code like:

propfull在编辑器中键入关键字,后跟两个TABs。它将生成如下代码:

private data_type var_name;

public data_type var_name1{ get;set;}

Video demonstrating the use of snippet 'propfull'(among other things), at 4 min 11 secs.

演示使用片段“propfull”(除其他外)的视频,时长为 4 分 11 秒。

回答by bsayegh

I created my own snippet that onlyadds {get; set;}. I made it just because I find propTabto be clunky.

我创建了自己的片段,添加{get; set;}. 我这样做只是因为我发现propTab很笨重。

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>get set</Title>
            <Shortcut>get</Shortcut>
        </Header>
        <Snippet>
            <Code Language="CSharp">
                <![CDATA[{get; set;}]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

With this, you type your PropType and PropName manually, then type getTab, and it will add the get set. It's nothing magical, but since I tend to type my access modifier first anyway, I may as well finish out the name and type.

有了这个,您手动输入您的 PropType 和 PropName,然后输入getTab,它将添加获取集。这没什么神奇的,但是因为无论如何我都倾向于首先输入我的访问修饰符,所以我最好完成名称和类型。