我们如何在 Visual Studio 中生成 getter 和 setter?
我所说的“生成”是指自动生成特定选定(一组)变量所需的代码。
但欢迎对良好实践进行任何更明确的解释或评论。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我所说的“生成”是指自动生成特定选定(一组)变量所需的代码。
但欢迎对良好实践进行任何更明确的解释或评论。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(16)
我个人使用 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?).
代表 Visual Studio 工具,我们可以使用名为 C# 属性生成器。
On behalf of the Visual Studio tool, we can easily generate C# properties using an online tool called C# property generator.
我的计算机上不再安装 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.
除了使用 Ctrl + K、X,您也可以只输入
prop
,然后按 Tab< /kbd> 两次。Rather than using Ctrl + K, X you can also just type
prop
and then hit Tab twice.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.
我使用 Visual Studio 2013 专业版。
您还将光标置于选择属性 。 使用菜单编辑→重构→封装字段...
其他信息:
自 C# 3.0(2007 年 11 月 19 日)起,我们可以使用自动实现的属性(这只是语法糖)。
还有
变成了
I use Visual Studio 2013 Professional.
You also place the cursor for choosing a property. Use menu Edit → Refactor → Encapsulate Field...
Other information:
Since C# 3.0 (November 19th 2007), we can use auto-implemented properties (this is merely syntactic sugar).
And
becomes
您还可以使用“propfull”并按两次TAB。
将生成具有 get 和 set 的变量和属性。
You can also use "propfull" and hit TAB twice.
The variable and property with get and set will be generated.
你所说的生成是指自动生成吗? 如果这不是您的意思:
Visual Studio 2008 对此有最简单的实现:
在后台,这会创建一个隐含的实例变量,您的属性将存储和检索到该变量。
但是,如果您想在属性中添加更多逻辑,则必须为其提供一个实例变量:
以前版本的 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:
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:
Previous versions of Visual Studio always used this longhand method as well.
从 Visual Studio 2019 开始,选择如下属性:
然后按
Ctrl+r
然后按
Ctrl+e
将出现一个对话框,向您显示代码将发生的更改的预览。 如果一切看起来都不错(大多数情况下都会如此),请按
确定
。As of visual studio 2019, select your properties like this:
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
.如果您使用的是 Visual Studio 2005 及更高版本,则可以使用插入片段命令快速创建 setter/getter。
右键单击您的代码,单击插入代码段(Ctrl+K、X),然后选择“列表中的“道具”。
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.
如果您使用的是 ReSharper,请进入 ReSharper 菜单 → 代码 → 生成...
(或按 Alt + Ins< /kbd> 在周围的类中),您将获得您能想到的用于生成 getter 和/或 setter 的所有选项:-)
If you're using ReSharper, go into the ReSharper menu → Code → Generate...
(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 :-)
在 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.
我创建了自己的代码段,仅添加了
{get; 设置;}
。 我这样做只是因为我发现prop
→ Tab 很笨拙。这样,您手动输入 PropType 和 PropName,然后输入
get
→ Tab,它将添加 get 集。 这没什么神奇的,但由于无论如何我都倾向于先输入访问修饰符,所以我也可以完成名称和类型。I created my own snippet that only adds
{get; set;}
. I made it just because I findprop
→ Tab to be clunky.With this, you type your PropType and PropName manually, then type
get
→ Tab, 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.使用
propfull
关键字。它将生成一个属性和一个变量。
在编辑器中键入关键字
propfull
,后跟两个 TAB。 它将生成如下代码:演示使用的视频片段“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:Video demonstrating the use of snippet 'propfull' (among other things), at 4 min 11 secs.
除了“prop”片段和自动属性之外,还有一个重构选项可让您选择现有字段并通过属性公开它(右键单击字段→重构→封装字段...)。
另外,如果您不喜欢“prop”实现,您可以创建自己的代码片段。 此外,像 ReSharper 这样的第三方重构工具将为您提供更多功能,并使更容易创建更高级的片段。 如果你能负担得起的话,我会推荐 ReSharper。
http://msdn.microsoft.com/ en-us/library/f7d3wz0k(VS.80).aspx
视频演示片段“prop”的使用(以及其他内容),位于3 分 23 秒。
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 → Refactor → Encapsulate 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.
http://msdn.microsoft.com/en-us/library/f7d3wz0k(VS.80).aspx
Video demonstrating the use of snippet 'prop' (among other things), at 3 min 23 secs.
首先获取扩展只需按 (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...