Reputation: 6699
So if I have a function like this in c# (#1)
private void Test(){
}
and if you hit Ctl K D, it changes to (#2)
private void Test()
{
}
Two questions here.
1. Is there any hot key combination to convert the code from #2 to #1?
2. Is there any recommendation from Micorosoft which one should be used. I prefer #2 but a colleague of mine prefer #1 and he thinks this is what microsoft recommend but never gave me any links. I personally care more about readability of code then recommendation but just curious if there is any recommendation like this.
Upvotes: 1
Views: 818
Reputation: 807
At the end of the day this is a personal preference... there are a few things to be sure of though:
Upvotes: 1
Reputation: 17946
"Preferable" is, of course, subjective, but if all you are interested in is consistency, Microsoft's recommendations are codified in StyleCop, which will automatically check code for you for things like this (and much more). I think it uses style #2, but outdent the braces, for what it is worth.
Assuming this page is accurate, here are some internal style guidelines. Item 2.2 answers your question (they use style #2, but not with the extra indent on the braces)
Upvotes: 2
Reputation: 7586
To convert from #2 to #1, go to Tools->Options->Text Editor->C#->Formatting->New Lines, change the appropriate items (in this case, "Place open brace on new line for methods"), OK out of the dialog, and hit Ctrl K D again
Upvotes: 3
Reputation: 7177
Preference, and it will depend on your business programming structure(if there is one in place). I myself perfer (number 2)
private void Test()
{
}
You can usually change the IDE to format whichever way you want though.
Upvotes: 0
Reputation: 26874
Code conventions are sometimes subjective. You may find that a company strictly imposes code stylings that are different from other companies/people.
To answer: 1. No but CTRL-Z right after you pressed CTRL-K,D (but if you save and close the file, no way back) 2. I don't know about no publication from Microsoft about that subject, but they use that code styling as recommended by VS and in all their code fragments. So that should be the recommended version
By the way, I would prefer a space in sample 1: private void Test() {
Upvotes: 0
Reputation: 1500893
Visual Studio comes with something like style #2 as the default, so that's what almost all C# code uses, in my experience. However, the braces in your example are indented for no obvious reason. More typical is:
private void Test()
{
foreach (int x in bar)
{
Console.WriteLine(x);
}
}
However, you can go into the Tools / Options / Languages / C# / Formatting and change it however you like. ReSharper gives even more options. Then you can reformat the code in your chosen style.
Upvotes: 2