views:

495

answers:

5

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

+5  A: 

With Resharper curly 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.

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 ;/

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 link about code formatting in Resharper.

MadBoy
ReSharper code format can also add braces throughout an entire project with just a few clicks. http://www.jetbrains.com/resharper/features/code_formatting.html
jrummell
Or remove them, depending on settings :-)
MadBoy
I am a huge Resharper fan.
William Edmondson
My Visual Studio without Resharper doesn't adds closing curly brackets
Jader Dias
Options -> Text Editor -> C# -> Formatting -> New Lines If that doesn't make it then i guess Resharper is the one making it ;)
MadBoy
A: 

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).

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.

Timores
+1  A: 

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

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

Carra
+2  A: 

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

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

Chris
+2  A: 

Devexpress have tools for visual studio

CodeRush

drorhan