C# 快捷方式或速记 getter setter

发布于 2024-12-13 12:41:12 字数 145 浏览 3 评论 0原文

有没有一种简单的方法可以在 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 技术交流群。

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

发布评论

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

评论(4

零度° 2024-12-20 12:41:12

是的,输入 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.

无可置疑 2024-12-20 12:41:12

如果您只是想要 getter 和 setter,按照最初的要求,您还可以创建自定义代码段:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>GetSet</Title>
            <Description>Inserts getter/setter shorthand code</Description>
            <Shortcut>gs</Shortcut>
        </Header>
        <Snippet>
            <Code Language="CSharp">
                <![CDATA[{ get; set; }$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

将以上内容另存为 .snippet 到您的代码段文件夹中。输入“gs”并按 Tab 键将插入 { get; set;} 并移动到该行的末尾。


在 VS Code 中编辑

,这将是 csharp 中的自定义用户代码段。 json 文件:

"Getter Setter": {
    "prefix": "gs",
    "body": [
        "\\{ get; set; \\}",
        "$1"
    ],
    "description": "Insert shorthand autoproperties"
}

这些示例中的任何一个都可以轻松修改/复制以执行 { get; } (使用 readonly 支持字段)或 { get;私人套装; }

If you just want the getter and setter, as orignally asked for, you could also create a custom snippet:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>GetSet</Title>
            <Description>Inserts getter/setter shorthand code</Description>
            <Shortcut>gs</Shortcut>
        </Header>
        <Snippet>
            <Code Language="CSharp">
                <![CDATA[{ get; set; }$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

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:

"Getter Setter": {
    "prefix": "gs",
    "body": [
        "\\{ get; set; \\}",
        "$1"
    ],
    "description": "Insert shorthand autoproperties"
}

Either of these examples could easily be modified/duplicated to also do { get; } (use a readonly backing field) or { get; private set; }

吃素的狼 2024-12-20 12:41:12

快捷方式是触发器“prop”:

prop<tab><tab>int<tab>Id<tab>

最终得到:

public int Id { get; set; }

The shortcut is the trigger "prop":

prop<tab><tab>int<tab>Id<tab>

and you end up with:

public int Id { get; set; }
北城孤痞 2024-12-20 12:41:12

当我输入 prop & TAB 我得到:

    public int MyProperty
    {
        get; set;
    }

有没有一种方法可以将其设置为全部在一行上,因此看起来像:

public int MyProperty { get;设置;}

更新!!!我想通了。

工具-->选项 -->文本编辑器 --> C# -->代码风格 -->格式化-->包装 -->勾选“将块保留在单行上”选项。它甚至使用“get;set;”举个例子。
设置获取;设置;为一行。

When I type prop & TAB I get:

    public int MyProperty
    {
        get; set;
    }

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.
Setting Get;Set; to one line.

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