在 Visual Studio 中创建构造函数的代码片段或快捷方式

发布于 2024-09-26 04:37:24 字数 77 浏览 8 评论 0 原文

在 Visual Studio 中创建构造函数的代码片段或快捷方式是什么?

Visual Studio 2010 和 C#。

What is the code snippet or shortcut for creating a constructor in Visual Studio?

Visual Studio 2010 and C#.

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

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

发布评论

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

评论(17

我ぃ本無心為│何有愛 2024-10-03 04:37:24

输入“ctor”+ TAB + TAB(按 Tab 键两次)。这将为您所在的类创建默认构造函数:

public MyClass()
{

}

似乎在某些情况下您必须按 TAB 两次。

Type "ctor" + TAB + TAB (hit the Tab key twice). This will create the default constructor for the class you are in:

public MyClass()
{

}

It seems that in some cases you will have to press TAB twice.

池木 2024-10-03 04:37:24

如果您想查看所有可用片段的列表:

Ctrl + K,然后按 X

If you want to see the list of all available snippets:

Press Ctrl + K and then X.

说谎友 2024-10-03 04:37:24

如果您想要一个带有属性的构造函数,您需要执行以下操作:

  1. 将光标放在类中的任何空行中;

  2. Ctrl + 触发快速操作重构菜单;

    重构菜单

  3. 从下拉菜单中选择生成构造函数

  4. 选择要作为构造函数参数包含的成员。您可以使用向上和向下箭头对它们进行排序。选择确定

构造函数是使用指定的参数创建的。

在 Visual Studio 中生成构造函数

In case you want a constructor with properties, you need to do the following:

  1. Place your cursor in any empty line in a class;

  2. Press Ctrl + . to trigger the Quick Actions and Refactorings menu;

    Refactoring menu

  3. Select Generate constructor from the drop-down menu;

  4. Pick the members you want to include as constructor parameters. You can order them using the up and down arrows. Choose OK.

The constructor is created with the specified parameters.

Generate a constructor in Visual Studio

水溶 2024-10-03 04:37:24

键入 ctor,然后按 TAB 两次。

Type ctor, and then press TAB twice.

ぶ宁プ宁ぶ 2024-10-03 04:37:24

要查看完整的代码片段列表(少量预制代码),请按 Ctrl+K,然后按 Ctrl+X
来源来自 MSDN
在 Visual Studio 2013 中使用 C# 项目。

那么如何制作构造

  1. 函数 按 Ctrl+K 然后 Ctrl+X
  2. 选择 V​​isual C#
  3. 选择 ctor
  4. 按 < kbd>Tab

更新:您还可以右键单击代码中需要代码段的位置,然后从右键单击菜单中选择“插入代码段”

For the full list of snippets (little bits of prefabricated code) press Ctrl+K and then Ctrl+X.
Source from MSDN.
Works in Visual Studio 2013 with a C# project.

So how to make a constructor

  1. Press Ctrl+K and then Ctrl+X
  2. Select Visual C#
  3. Select ctor
  4. Press Tab

Update: You can also right-click in your code where you want the snippet, and select Insert Snippet from the right-click menu

提笔书几行 2024-10-03 04:37:24

在 Visual Studio 2010 中,如果您键入“ctor”(不带引号),则应加载 IntelliSense,并在列表中显示“ctor”。现在按 TAB 两次,您应该生成一个空的构造函数。

In Visual Studio 2010, if you type "ctor" (without the quotes), IntelliSense should load, showing you "ctor" in the list. Now press TAB twice, and you should have generated an empty constructor.

你没皮卡萌 2024-10-03 04:37:24

如果您使用 ReSharper,则可以通过键入以下内容快速生成构造函数:

  • 'ctor' + Tab + Tab(不带参数)、
  • 'ctorf' + Tab + Tab (带有初始化所有字段的参数)或
  • 'ctorp' + Tab + Tab (带有初始化所有属性的参数)。

If you use ReSharper, you can quickly generate constructors by typing:

  • 'ctor' + Tab + Tab (without parameters),
  • 'ctorf' + Tab + Tab (with parameters that initialize all fields) or
  • 'ctorp' + Tab + Tab (with parameters that initialize all properties).
像你 2024-10-03 04:37:24

正如许多人提到的,“ctor”和双 TAB 在 Visual Studio 2017 中工作,但它只创建没有任何属性的构造函数。

要自动生成属性(如果有),只需单击属性下方的空行,然后按 Ctrl + .。它将显示一个小弹出窗口,您可以从中选择“生成构造函数...”选项。

As mentioned by many, "ctor" and double TAB works in Visual Studio 2017, but it only creates the constructor with none of the attributes.

To auto-generate with attributes (if there are any), just click on an empty line below them and press Ctrl + .. It'll display a small pop-up from which you can select the "Generate Constructor..." option.

零崎曲识 2024-10-03 04:37:24

只需输入 ctor,然后按 TAB

Simply type ctor then press TAB.

疯狂的代价 2024-10-03 04:37:24

如果您有兴趣从头开始创建“ctor”或类似的类名注入代码片段,请在 C# 代码片段目录中创建一个 .snippet 文件(例如 C:\VS2017\VC#\Snippets\1033\ Visual C#\C#Snippets.snippet) 包含以下 XML 内容:

<CodeSnippets>
    <CodeSnippet>
        <Header>
            <Title>ctor</Title>
            <Shortcut>ctor</Shortcut>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="false"><ID>classname</ID><Function>ClassName()</Function></Literal>
            </Declarations>
            <Code>
                <![CDATA[public $classname$($end$)
                {

                }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

此代码片段通过调用 C# 代码片段函数 ClassName() 来注入当前类名称,详细信息请参阅 此 docs.microsoft 页面

展开此代码片段的最终结果:

'ctor' snippet

构造函数最终结果

Should you be interested in creating the 'ctor' or a similar class-name-injecting snippet from scratch, create a .snippet file in the C# snippets directory (for example C:\VS2017\VC#\Snippets\1033\Visual C#\C#Snippets.snippet) with this XML content:

<CodeSnippets>
    <CodeSnippet>
        <Header>
            <Title>ctor</Title>
            <Shortcut>ctor</Shortcut>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="false"><ID>classname</ID><Function>ClassName()</Function></Literal>
            </Declarations>
            <Code>
                <![CDATA[public $classname$($end$)
                {

                }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

This snippet injects the current class name by way of calling C# code snippet function ClassName(), detailed on this docs.microsoft page.

The end result of expanding this code snippet:

'ctor' snippet

Constructor end result

全部不再 2024-10-03 04:37:24
  1. Alt + Enter
  2. 选择“生成构造函数”
  3. 选择所需成员 使用

所选成员生成参数化构造函数。

  1. Press Alt + Enter
  2. Select "Generate Constructor"
  3. Select required members

A parameterized constructor is generated with the selected members.

慈悲佛祖 2024-10-03 04:37:24

我不了解 Visual Studio 2010,但在 Visual Studio 2008 中,代码片段是“ctor”。

I don't know about Visual Studio 2010, but in Visual Studio 2008 the code snippet is 'ctor'.

黯然#的苍凉 2024-10-03 04:37:24

键入ctor,然后按Tab 键。

Type ctor, and then press the Tab key.

谁把谁当真 2024-10-03 04:37:24

键入ctorTab

ََََََََََ

Type ctor and Tab.

ََََََََََ

孤寂小茶 2024-10-03 04:37:24

输入任何代码片段的名称并按 TAB

要获取属性代码,您需要选择正确的选项并按 TAB 两次,因为 Visual Studio 有多个以“prop”开头的选项,例如“prop”、“propa”和“propdp” '。

Type the name of any code snippet and press TAB.

To get code for properties you need to choose the correct option and press TAB twice because Visual Studio has more than one option which starts with 'prop', like 'prop', 'propa', and 'propdp'.

故人如初 2024-10-03 04:37:24

我创建了一些方便的代码片段,它们也将创建重载的构造函数。欢迎您使用它们:https://github.com/ejbeaty/Power-Snippets

例如:“ctor2”将创建一个带有两个参数的构造函数,并允许您像这样逐个浏览它们:

public MyClass(ArgType argName, ArgType argName)
{

}

I have created some handy code snippets that'll create overloaded constructors as well. You're welcome to use them: https://github.com/ejbeaty/Power-Snippets

For example: 'ctor2' would create a constructor with two arguments and allow you to tab through them one by one like this:

public MyClass(ArgType argName, ArgType argName)
{

}
鹿港巷口少年归 2024-10-03 04:37:24

对于 Visual Studio 2017,请按 Ctrl + .

For Visual Studio 2017, press Ctrl + ..

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