visual-studio 如何让 Visual Studio 在 C# 代码中自动包含大括号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2355838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:36:35  来源:igfitidea点击:

How to make Visual Studio to automatically include curly brackets in C# code?

c#visual-studiovisual-studio-2008syntax

提问by Jader Dias

After namespace, class, struct, enum, for, foreach, while, switch, do, using, uncheckedand at the beginning of methods, shouldn't Visual Studio automatically include curly brackets? How to activate this feature?

namespace, class, struct, enum, for, foreach, while, switch, do, using,unchecked和方法的开头,Visual Studio 不应该自动包含大括号吗?如何激活此功能?

采纳答案by Chris Pitman

One option is to use Code Snippets. Visual Studio already comes with snippets for namespace, enum, for, foreach, while, switch, do, using, and unchecked. For instructions on how to use snippets go here.

一种选择是使用代码片段。Visual Studio 已经提供了命名空间、枚举、for、foreach、while、switch、do、using 和 unchecked 的片段。有关如何使用代码段的说明,请访问此处

If you can think of a snippet that makes sense for your class and struct you can create your own snippet.

如果您能想到对您的类和结构有意义的代码段,您可以创建自己的代码段

回答by MadBoy

With Resharpercurly closing bracket adds by itself (either right away or after pressing enter). I think it's also the case for non-resharped Visual Studio but not 100% sure about that. Only opening bracket you have to write by yourself.

使用Resharper卷曲结束括号自行添加(立即或按 Enter 键后)。我认为非重新锐化的 Visual Studio 也是这种情况,但不能 100% 确定。只有开括号你必须自己写。

Currently Resharper 5.0 is free (as long as it's in beta/night build status), you just have to reinstall it every 20-30 days (but you would want to do it anyway since they tend to fix/add things). I can't code without Resharper anymore :-) It's so much time saver! Not sure what i will do when it's no longer beta ;/

目前 Resharper 5.0 是免费的(只要它处于测试版/夜间构建状态),您只需要每 20-30 天重新安装一次(但您无论如何都想这样做,因为它们往往会修复/添加东西)。如果没有 Resharper,我就无法再编码了 :-) 这节省了很多时间!不知道当它不再是测试版时我会做什么;/

Edit:

编辑:

With Resharper you can even get IDE to use braces or not to use them in some cases (where it's possible). Check out this linkabout code formatting in Resharper.

使用 Resharper,您甚至可以让 IDE 在某些情况下(在可能的情况下)使用或不使用大括号。查看有关 Resharper 中代码格式的链接

回答by Orhan Cinar

Devexpress have tools for visual studio

Devexpress 有 Visual Studio 的工具

CodeRush

CodeRush

回答by user1351333

If you press CTRL+K, CTRL+S this will give you a snippet of options in which it will automatically create whichever option you select.

如果您按 CTRL+K、CTRL+S,这将为您提供一个选项片段,它会在其中自动创建您选择的任何选项。

回答by Du?an Kalivoda

It is very easily and you have not to use R#.

这很容易,您不必使用 R#。

Only write "if, else, for, foreach..." and then press TAB. Curly brackets will be automatically created and your cursor jump to round brackets. If in round brackets is more controls (e.g. for(control1; control2; control3)), then you can switch between this controls with press TAB. If you finished with changing controls, then you can press enter and cursor will jump to body of curly brackets.

只写“if, else, for, foreach...”然后按TAB。将自动创建大括号,并且您的光标会跳转到圆括号。如果圆括号中有更多控件(例如 for(control1; control2; control3)),那么您可以按 TAB 在这些控件之间切换。如果您完成更改控件,那么您可以按 Enter 键,光标将跳转到大括号的主体。

But if do you want write new "method, class,..." I usually use general shortcut "ctrl+.".

但是如果你想写新的“方法,类,...”我通常使用通用快捷方式“ ctrl+.”。

e.g. Write to some method (e.g. MyMethod) call new method (e.g. MyNewMethod) which not be created yet:

例如写入某些方法(例如 MyMethod)调用尚未创建的新方法(例如 MyNewMethod):

private void MyMethod()
{
  string[] a = this.MyNewMethod(5, "something");
}

Then set cursor to "MyNewMethod", press "ctrl+." and press enter.

然后将光标设置为“MyNewMethod”,按“ctrl+”。并按回车键。

New method will be created automatically on the end of this class:

新方法将在此类结束时自动创建:

private void MyMethod()
{
  string[] a = this.MyNewMethod(5, "something");
}

private string[] MyNewMethod(int v1, string v2)
{
  throw new NotImplementedException();
}

回答by Carra

The IDE can't be sure if you don't want to create a one liner.

IDE 无法确定您是否不想创建单衬。

Resharper does make things easier though. If you type "{" and press enter the "}" will automatically be generated.

不过,Resharper 确实让事情变得更容易了。如果您键入“{”并按回车键,“}”将自动生成。

回答by Timores

For namespace, class, struct, enum and switch, you're right. The syntax forces you to get an opening brace, but I don't think you can force VS to do this (without using an add-in).

对于命名空间、类、结构、枚举和开关,您是对的。语法强制您使用左大括号,但我认为您不能强制 VS 执行此操作(不使用加载项)。

In the other cases, an open brace is optional, and some people want NOT to have the brace if they have only one statement afterwards. So, VS could not add a brace automatically even if it could.

在其他情况下,开大括号是可选的,如果之后只有一个语句,有些人不希望有大括号。因此,即使可以,VS 也无法自动添加大括号。