C# 快捷方式或速记 getter setter
有没有一种简单的方法可以在 C# 中创建 getter 和 setter?
public string fname {get; set;}
是否有生成 {get; 的简写设置;}
?
Is there a short way to create the getter and setter in c#?
public string fname {get; set;}
Is there short hand to generate {get; set;}
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,输入 prop 并按 TAB 键。 Visual Studio 有一个自动属性的片段。
对于具有 public get 和 private set 的属性,您可以使用 propg 并按 TAB。
对于完整的非自动属性,您可以使用 propfull 并按 TAB 键。
yea type prop and press TAB. Visual Studio has a snippet for automatic property.
For property with public get and private set, you can use propg and press TAB.
For complete non auto property you can use propfull and press TAB.
如果您只是想要 getter 和 setter,按照最初的要求,您还可以创建自定义代码段:
将以上内容另存为 .snippet 到您的代码段文件夹中。输入“gs”并按 Tab 键将插入
{ get; set;}
并移动到该行的末尾。在 VS Code 中编辑
,这将是
csharp 中的自定义用户代码段。 json
文件:这些示例中的任何一个都可以轻松修改/复制以执行
{ get; }
(使用readonly
支持字段)或{ get;私人套装; }
If you just want the getter and setter, as orignally asked for, you could also create a custom snippet:
Save the above as a .snippet in your snippets folder. Typing 'gs' and pressing Tab will insert
{ get; set;}
and move to the end of the line.Edit
In VS Code, this would be a custom user snippet in your
csharp.json
file:Either of these examples could easily be modified/duplicated to also do
{ get; }
(use areadonly
backing field) or{ get; private set; }
快捷方式是触发器“prop”:
最终得到:
The shortcut is the trigger "prop":
and you end up with:
当我输入 prop & TAB 我得到:
有没有一种方法可以将其设置为全部在一行上,因此看起来像:
public int MyProperty { get;设置;}
更新!!!我想通了。
工具-->选项 -->文本编辑器 --> C# -->代码风格 -->格式化-->包装 -->勾选“将块保留在单行上”选项。它甚至使用“get;set;”举个例子。

When I type prop & TAB I get:
Is there a way to set it up so it's all on one line so it looks like:
public int MyProperty { get; set;}
UPDATE!!! I figured it out.
Tools --> Options --> Text Editor --> C# --> Code Style --> Formatting --> Wrapping --> Put a checkmark in the "Leave block on a single line" option. It even uses the "get; set;" as an example.
