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

发布于 2024-07-04 15:59:20 字数 70 浏览 5 评论 0 原文

我所说的“生成”是指自动生成特定选定(一组)变量所需的代码。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

盗梦空间 2024-07-11 15:59:20

我个人使用 CTRL+. 然后选择-
“封装的字段”。
这是此选项的缩写 - (我们如何在 Visual Studio 中生成 getter 和 setter?)。

I personaly use CTRL+. and then select-
"Encapsulated Fildes".
That's a short for this option- (How can we generate getters and setters in Visual Studio?).

双马尾 2024-07-11 15:59:20

输入图片此处描述

代表 Visual Studio 工具,我们可以使用名为 C# 属性生成器。

Enter image description here

On behalf of the Visual Studio tool, we can easily generate C# properties using an online tool called C# property generator.

喜你已久 2024-07-11 15:59:20

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

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

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

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.

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.

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

追我者格杀勿论 2024-07-11 15:59:20

除了使用 Ctrl + KX,您也可以只输入 prop,然后按 Tab< /kbd> 两次。

Rather than using Ctrl + K, X you can also just type prop and then hit Tab twice.

油焖大侠 2024-07-11 15:59:20

Visual Studio 还具有从私有变量生成属性的功能。

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

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

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

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.

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.

陌伤ぢ 2024-07-11 15:59:20

我使用 Visual Studio 2013 专业版。

  • 将光标置于实例变量所在行。

    在此处输入图像描述

  • 按组合键 Ctrl + R Ctrl + E,或单击鼠标右键。 选择上下文菜单重构封装字段...,然后按确定

    在此处输入图像描述

  • 预览参考更改 - 封装字段对话框中,按按钮应用

    在此处输入图像描述

  • 这是结果:

    在此处输入图像描述

您还将光标置于选择属性 。 使用菜单编辑重构封装字段...

  • 其他信息:

    自 C# 3.0(2007 年 11 月 19 日)起,我们可以使用自动实现的属性(这只是语法糖)。

    还有

    private int ProductID; 
    
      公共 int 产品 ID 
      { 
          获取{返回产品ID;   } 
          设置 { 产品ID = 值;   } 
      } 
      

    变成了

    public int ProductID { get;   放;   } 
      

I use Visual Studio 2013 Professional.

  • Place your cursor at the line of an instance variable.

    Enter image description here

  • Press combine keys Ctrl + R, Ctrl + E, or click the right mouse button. Choose context menu RefactorEncapsulate Field..., and then press OK.

    Enter image description here

  • In Preview Reference Changes - Encapsulate Field dialog, press button Apply.

    Enter image description here

  • This is result:

    Enter image description here

You also place the cursor for choosing a property. Use menu EditRefactorEncapsulate Field...

  • Other information:

    Since C# 3.0 (November 19th 2007), we can use auto-implemented properties (this is merely syntactic sugar).

    And

    private int productID;
    
    public int ProductID
    {
        get { return productID; }
        set { productID = value; }
    }
    

    becomes

    public int ProductID { get; set; }
    
独留℉清风醉 2024-07-11 15:59:20

您还可以使用“propfull”并按两次TAB

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

You can also use "propfull" and hit TAB twice.

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

嗫嚅 2024-07-11 15:59:20

你所说的生成是指自动生成吗? 如果这不是您的意思:

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

public PropertyType PropertyName { get; set; }

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

但是,如果您想在属性中添加更多逻辑,则必须为其提供一个实例变量:

private PropertyType _property;

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

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

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

Visual Studio 2008 has the easiest implementation for this:

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.

双马尾 2024-07-11 15:59:20

从 Visual Studio 2019 开始,选择如下属性:

在此处输入图像描述

然后按 Ctrl+r
然后按 Ctrl+e

将出现一个对话框,向您显示代码将发生的更改的预览。 如果一切看起来都不错(大多数情况下都会如此),请按确定

As of visual studio 2019, select your properties like this:

enter image description here

Then press Ctrl+r
Then press Ctrl+e

A dialog will appear showing you the preview of the changes that are going to happen to your code. If everything looks good (which it mostly will), press OK.

未央 2024-07-11 15:59:20

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

右键单击您的代码,单击插入代码段Ctrl+KX),然后选择“列表中的“道具”。

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

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

终难愈 2024-07-11 15:59:20

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

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

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

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

妄司 2024-07-11 15:59:20

在 Visual Studio Community Edition 2015 中,您可以选择所需的所有字段,然后按 Ctrl + . 自动生成属性。

您必须选择是否要使用属性而不是字段。

In Visual Studio Community Edition 2015 you can select all the fields you want and then press Ctrl + . to automatically generate the properties.

You have to choose if you want to use the property instead of the field or not.

小清晰的声音 2024-07-11 15:59:20

我创建了自己的代码段,添加了 {get; 设置;}。 我这样做只是因为我发现 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>

这样,您手动输入 PropType 和 PropName,然后输入 getTab,它将添加 get 集。 这没什么神奇的,但由于无论如何我都倾向于先输入访问修饰符,所以我也可以完成名称和类型。

I created my own snippet that only adds {get; set;}. I made it just because I find propTab to be clunky.

<?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.

帅的被狗咬 2024-07-11 15:59:20

使用 propfull 关键字。

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

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

private data_type var_name;

public data_type var_name1{ get;set;}

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

Use the propfull keyword.

It will generate a property and a variable.

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

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.

疯了 2024-07-11 15:59:20

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

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

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...).

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

空气里的味道 2024-07-11 15:59:20

首先获取扩展只需按 (Ctrl + Shift + X) 并安装 getter setter ....

之后,只需选择您的变量并右键点击。 转到命令选项板...

并输入 getter ...它将建议生成 get 和 set 方法。 点击这个...

First get Extension just press (Ctrl + Shift + X) and install getter setter ....

After this, just select your variable and right click. Go to Command palette...

And type getter ... It will suggest generate get and set methods. Click on this...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文