none
Visual Studio 2019 Preview C# Curly Braces issues RRS feed

  • Question

  • So recently I have tried out C#, and I am taking an online tutorial on it. I got the 2019 preview, but I do not know how to format the curly braces the correct way for methods, if statements, etc. I have looked at the forum before for answers, yet they didn't work. I looked in the options, and everything was fine. Any help?
    Tuesday, April 30, 2019 1:59 AM

All replies

  • Greetings PattyHitsNubs.

    You may have to be a bit more specific. Can you give us a small example of code you say "doesn't work"?

    Generally, code will compile as long as there is a closing brace for every opening brace. After that, it's usual to have the opening braces line up in the same column as their corresponding closing brace, with the code in between indented by (usually) three spaces.

    void AMethod()
    { // Opening brace for the method
       // Code in the method indented 3 spaces.
       if(somethingOrOther)
       { // Opening brace for the 'if'.
          int i = GetSomething(); // Code in the 'if' indented another 3 spaces.
       } // closing brace for the 'if'.
    } // Closing brace for the method.

    Tuesday, April 30, 2019 2:17 AM
  • Hi PattyHitsNubs,

    Thank you for posting here.

    Please provide more details. Code with error message would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 30, 2019 8:34 AM
    Moderator
  • Hi Wendy,

    Thanks for your patience, I was in a rush then and haven't had time lately.

    Here is the code of the entire thing:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Working_on_it
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool isBigger = true;
                if (isBigger) {}


                Console.ReadLine();
            }


        }
    }


    What I was wondering, mostly, was how to make those curly braces like that become properly formatted immediately. I would see people do that and wonder how, so do you know how to? 

    ex. of what I want it to do right away:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Working_on_it
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool isBigger = true;
                if (isBigger) 

                {

                }

                Console.ReadLine();
            }


        }
    }

    Thanks again!
    -PattyHitsNubs

    Thursday, May 2, 2019 12:57 AM
  • There were also no error messages.
    Thursday, May 2, 2019 12:58 AM
  • Greetings again Patty.

    I'm still not certain what you are after, but you can set up the options for indentation as follows.

    It's recommended you insert spaces rather than tabs, as above.

    If you are dealing with code that hasn't been formatted correctly, there are a couple of tools under Edit->Advanced that can help. You can format a whole document in one go, or select a few lines and format just them. After a while you should be able to remember the shortcuts, Ctrl+K, Ctrl+D and Ctrl+K, Ctrl+F respectively.

    Thursday, May 2, 2019 1:48 AM
  • Hi Ante,

    None of your solutions worked for me so far.

    My problem, although I do not know the name, is where when I press enter in the middle of a set of curly braces after, for example, an if statement, instead of formatting correctly in the right way, the closing curly brace will go to the start of the first line and the opening curly brace will stay there, just like how normal text editing, in notepad or something, does. The options for this are in:
    Text Editor>C#>Code Style>Formatting>New Lines

    Hope this helps!

    Thanks,
    PattyHitsNubs

    (I'm sorry someone thought this was an answer, it was me trying to give the necessary details out for anyone to give me some clue of what to do next)


    Thursday, May 2, 2019 2:24 AM