6

I recently upgraded to Visual Studio 2015 (update 1) and have been getting things configured as I want them to be, however I am having a little trouble with formatting with regards to curly braces! I am under the assumption that the 2nd code sample is the more accepted convention, yet I can not seem to get my settings right. What am I doing wrong here?

Visual Studio is currently auto-formatting my code like this, notice the else {

    public static string Gender(string x)
    {
        if (x == "M")
        {
            return "Male";
        }
        else if (x == "F")
        {
            return "Female";
        }
        else {
            return "Unknown";
        }
    }

I would like my code to be auto-formatted like this

    public static string Gender(string x)
    {
        if (x == "M")
        {
            return "Male";
        }
        else if (x == "F")
        {
            return "Female";
        }
        else
        {
            return "Unknown";
        }
    }
GTRocker
  • 61
  • 1
  • 5
  • 2
    Tools > Options > Text Editor > C# > Formatting > New Lines > [Enable] Place open brace on new line for control blocks. i think this should work. – M.kazem Akhgary Dec 31 '15 at 20:20
  • 1
    This seems to be broken in VS2015 update 1. It doesn't seem to care which line the `{` following the `else` is on, and won't auto-format it with either of the above styles (given default settings). Doesn't seem to operate the same way as VS2012 at least, which is the only comparison point I have available. – Glorin Oakenfoot Dec 31 '15 at 20:29
  • resharper will fix this. while giving you a lot of useful features to write your code faster. when i installed VS 2015 first thing i did was to install resharper! so i didnt notice this problem ;) https://www.jetbrains.com/resharper/ – M.kazem Akhgary Dec 31 '15 at 20:38
  • I hope they fix this soon because it drives me CRAZY!!! – Xtro Jan 26 '16 at 16:09
  • I was seeking a solution for this, then I found your question here. Did you find a solution for this yet? Thank you. – Nuno Apr 07 '16 at 22:34
  • This is fixed in Update 2 – James Jun 10 '16 at 13:31

1 Answers1

3

I have Visual Studio 2015 Update 1 and have it set up to format like in your second code snippet.

An important setting is to uncheck "place else on new line" option.

If this is not enough, I am posting a screenshot with all my settings (https://i.stack.imgur.com/sdzTZ.jpg)

enter image description here

George
  • 2,436
  • 4
  • 15
  • 30