在 Visual Studio 中创建属性的快捷方式?

发布于 2024-09-26 11:08:57 字数 196 浏览 1 评论 0原文

我见过一些人在 C# 中创建属性的速度非常快,但他们是如何做到的呢?

Visual Studio(当前使用 Visual Studio 2010)中可以使用哪些快捷方式来创建属性?

我正在使用 C#。

例如,

public string myString {get;set;}

I have seen some people creating properties in C# really fast, but how did they do it?

What shortcuts are available in Visual Studio (currently using Visual Studio 2010) to create properties?

I am using C#.

For example,

public string myString {get;set;}

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

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

发布评论

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

评论(17

叹倦 2024-10-03 11:08:57

您可以输入“prop”,然后按 Tab 键两次。这将生成以下内容。

public TYPE Type { get; set; }

然后更改“TYPE”和“Type”:

public string myString {get; set;}

您还可以输入“propfull”并按 Tab 两次来获取完整属性。这将生成该字段和完整的财产。

private int myVar;

public int MyProperty
{
    get { return myVar;}
    set { myVar = value;}
}

You could type "prop" and then press tab twice. That will generate the following.

public TYPE Type { get; set; }

Then you change "TYPE" and "Type":

public string myString {get; set;}

You can also get the full property typing "propfull" and then tab twice. That would generate the field and the full property.

private int myVar;

public int MyProperty
{
    get { return myVar;}
    set { myVar = value;}
}
素染倾城色 2024-10-03 11:08:57

除了 Amra 的答案,您还可以 查找其他片段

通过键入Ctrl + KCtrl + X

,这在我的 Visual 中映射到 Edit.InsertSnippet Studio 并向您显示可用片段的完整列表。

另请记住,您可以使用代码片段管理器配置您自己的代码片段,该代码片段位于工具菜单的代码片段管理器...中。
基本上,您创建一个文件 *.snippet 并使用代码片段管理器中的“导入”按钮将其添加到 Visual Studio。
要获得完整的教程,您可以访问文档; 演练:创建代码片段


在 Visual Studio 中,代码片段的处理方式与 Visual Studio 中略有不同。您可以通过输入 Ctrl + Shift + P 并输入 snippet 来访问所有代码段。应该有两个选项可用:插入片段首选项:配置用户片段

前者从片段列表中插入片段(使用您可以在状态栏中看到的语言模式),而使用后者您可以为任何语言模式创建自己的片段。

如果您知道短名称,则只需键入该短名称并使用 Tab 即可展开代码片段。为了插入 C# 属性,您可以使用三个代码段:proppropfullpropg,用于不同的目的。

In addition to Amra's answer, you can find other snippets by typing

Ctrl + K, Ctrl + X

Which is mapped to Edit.InsertSnippet in my Visual Studio and shows you the full list of snippets available.

Also remember that you can configure your own snippets by using the Snippets Manager, which is available in the Tools menu, Code Snippets Manager....
Basically you create a file *.snippet and use the Import button in the Code Snippets Manager to add it to Visual Studio.
For a full tutorial you can go to the docs; Walkthrough: Create a code snippet.


In Visual Studio Code snippets are handled slightly different than in Visual Studio. You can access all snippets by typing Ctrl + Shift + P and type in snippet. Two options should be available, Insert Snippet and Preferences: Configure User Snippets.

The former inserts a snippet from your list of snippets (using the Language Mode which you can see in the status bar), and with the latter you can create your own snippets for any Language Mode.

If you know the shortname you can just type that and use Tab to expand the snippet. For inserting a C# property you have three snippets available, prop, propfull, and propg, for different purposes.

笑着哭最痛 2024-10-03 11:08:57

将光标放在字段内private int _i;,然后编辑菜单或人民币 - 重构 - 封装字段... (CtrlR, CtrlE) 创建标准属性访问器。

Place cursor inside your field private int _i; and then Edit menu or RMB - Refactor - Encapsulate Field... (CtrlR, CtrlE) to create the standard property accessors.

放低过去 2024-10-03 11:08:57

输入“propfull”。使用起来比较好,它会生成属性和私有变量。

输入“propfull”,然后按 TAB 两次。

Type "propfull". It is much better to use, and it will generate the property and private variable.

Type "propfull" and then TAB twice.

琉璃繁缕 2024-10-03 11:08:57

按照 Amra 的建议输入 "prop" + Tab + Tab 后,
您可以立即输入属性的类型(这将替换默认的 int),输入另一个选项卡并输入属性名称(这将替换默认的 MyProperty)。按 Enter 完成。

After typing "prop" + Tab + Tab as suggested by Amra,
you can immediately type the property's type (which will replace the default int), type another tab and type the property name (which will replace the default MyProperty). Finish by pressing Enter.

心不设防 2024-10-03 11:08:57

开始于:

private int myVar;

当您选择“myVar”并右键单击然后选择“重构”并选择“封装字段”时。

它将自动创建:

{
    get { return myVar; }
    set { myVar = value; }
}

或者您可以通过按 Ctrl + R + E 快捷方式创建它。

Start from:

private int myVar;

When you select "myVar" and right click then select "Refactor" and select "Encapsulate Field".

It will automatically create:

{
    get { return myVar; }
    set { myVar = value; }
}

Or you can shortcut it by pressing Ctrl + R + E.

攒眉千度 2024-10-03 11:08:57

我认为 Alt+R+F 是从变量声明创建属性的正确方法

I think Alt+R+F is the correct one for creating property from a variable declaration

季末如歌 2024-10-03 11:08:57

我在 IDE 中喜欢的一点是,我能够编写一些变量,例如:

    private int id;
    private string name;
    private string version;
    private string description;
    private string status;
    private string symbol;

注意,变量名称以小写字母开头,然后选择整个块,然后按 Ctrl+ RCtrl+E、应用。属性是用大写字母生成的:

    public int Id
    {
        get
        {
            return id;
        }

        set
        {
            id = value;
        }
    }

等。

What I liked in the IDE was that I was able to write a few variables like:

    private int id;
    private string name;
    private string version;
    private string description;
    private string status;
    private string symbol;

Notice, that the variable names start with small letters, and then select the whole block, and press Ctrl+R, Ctrl+E, Apply. The properties are generated with the capital letter:

    public int Id
    {
        get
        {
            return id;
        }

        set
        {
            id = value;
        }
    }

etc.

荭秂 2024-10-03 11:08:57

键入 P + Tab + Tab

更改数据类型,按 TAB,更改属性名称,然后按 End + Enter

Type P + Tab + Tab.

Change the datatype, press TAB, change the property name, and press End + Enter.

旧竹 2024-10-03 11:08:57

当您在 Visual Studio 中编写时,

public ServiceTypesEnum Type { get; set; }
public string TypeString { get { return this.Type.ToString();}}

ReSharper 会不断建议将其转换为:

public string TypeString => Type.ToString();

When you write in Visual Studio,

public ServiceTypesEnum Type { get; set; }
public string TypeString { get { return this.Type.ToString();}}

ReSharper will keep suggesting to convert it to:

public string TypeString => Type.ToString();
过度放纵 2024-10-03 11:08:57

转到

工具>>选项>>文本编辑器>> C# >> IntelliSense

在“代码片段行为”部分下:

确保选中“始终包含代码片段”。

我希望它也适合你。

Go to

Tools >> Options >> Text Editor >> C# >> IntelliSense

Under the Snippets behaviour section:

Make sure "Always include snippets" is selected.

I hope it works for you too.

白首有我共你 2024-10-03 11:08:57

如果您使用的是 Visual Studio 2013、2015 或更高版本,只需单击下面的链接。它将为您提供 Visual Studio 中的完整快捷方式!

Visual C# 代码片段

If you are using Visual Studio 2013, 2015 or above, just click the link below. It will give you the full shortcuts in Visual Studio!

Visual C# Code Snippets

撩发小公举 2024-10-03 11:08:57

ReSharper 提供属性生成在其广泛的功能集中。 (不过它并不便宜,除非你正在开发一个开源项目。)

ReSharper offers property generation in its extensive feature set. (It's not cheap though, unless you're working on an open-source project.)

美羊羊 2024-10-03 11:08:57

使用 VsVim 的代码片段似乎有点有趣。当我到达这里时,我正在寻找的快捷方式要简单得多:在成员名称之后输入 {g;s;

我打开了分隔符自动关闭功能,因此右大括号出现在 { 上,并且键入分号会触发 get 和 set 的自动完成。

它适用于 VS2013 和 VS2015,VS2012 只是缺少自动大括号匹配。

Using VsVim the code snippets seem to work a little funny. The shortcut I was looking for when I ended up here is much simpler: after a member name type {g;s;

I have delimiter auto-closing turned on, so the closing brace appears on {, and typing a semicolon triggers an autocomplete for get and set.

It works on VS2013 and VS2015, and VS2012 just lacks the automatic brace matching.

太阳哥哥 2024-10-03 11:08:57

在 Visual Studio 2017 社区中,按键是 ctrl + 。

In visual studio 2017 community, the key is ctrl + .

长安忆 2024-10-03 11:08:57

在 C# 中:

private string studentName;

在分号(;)之后的行尾,只需按下

Ctrl + R + E

它就会显示一个弹出窗口,如下所示:
输入图片此处描述
单击“应用”或按 ENTER 后,将生成以下属性代码:

public string StudentName
        {
            get
            {
                return studentName;
            }

            set
            {
                studentName = value;
            }
        }

在 VB 中:

Private _studentName As String

在行尾(在字符串之后)按,确保将 _(下划线)放在开头,因为它将添加数字在属性末尾:

Ctrl + R + E

将出现相同的窗口:
输入图片此处描述

单击“Apply”或按“ENTER”键,将生成以下带有数字的属性代码,如下所示:

Public Property StudentName As String
        Get
            Return _studentName
        End Get
        Set(value As String)
            _studentName = value
        End Set
    End Property

带数字的属性如下所示:

Private studentName As String
 Public Property StudentName1 As String
        Get
            Return studentName
        End Get
        Set(value As String)
            studentName = value
        End Set
    End Property

In C#:

private string studentName;

At the end of line after semicolon(;) Just Press

Ctrl + R + E

It will show a popup window like this:
enter image description here
On click of Apply or pressing of ENTER it will generate the following code of property:

public string StudentName
        {
            get
            {
                return studentName;
            }

            set
            {
                studentName = value;
            }
        }

In VB:

Private _studentName As String

At the end of line (after String) Press, Make sure you place _(underscore) at the start because it will add number at the end of property:

Ctrl + R + E

The same window will appear:
enter image description here

On click of Apply or pressing of ENTER it will generate the following code of property with number at the end like this:

Public Property StudentName As String
        Get
            Return _studentName
        End Get
        Set(value As String)
            _studentName = value
        End Set
    End Property

With number properties are like this:

Private studentName As String
 Public Property StudentName1 As String
        Get
            Return studentName
        End Get
        Set(value As String)
            studentName = value
        End Set
    End Property
野侃 2024-10-03 11:08:57

您可以通过按以下方式定义字段:
Ctrl+. 并通过选择封装字段:[..]

输入图片此处描述

You can define a field by pressing:
Ctrl+. and insert its property by selecting Encapsulate field: [..].

enter image description here

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