[SOLVED] VSCode format curly brackets on the same line c# – check simple solution in 2 mins!

VSCode format curly brackets on the same line c#
Share this post and Earn Free Points!

[ VSCode" format curly brackets on the same line c# ] If you work with other programming" languages such as Java or Scala, you have probably been pissed off more than once about how the code in C# is formatted by default". Any method / function, if-else block, or other syntax resulted in more than one line causing a brace to appear on an additional line. See the example below:


Visual Studio Code (VSCode)

Visual Studio Code" (VSCode") is a free and open-source code editor developed by Microsoft for Windows", Linux", and macOS. It is designed to support a wide range of programming languages and development scenarios, and includes features such as syntax highlighting, code completion, debugging, and source control integration.

VSCode" is popular among developers for its speed, simplicity, and extensibility. It includes a wide range of extensions that can be installed to add additional features and functionality to the editor. Some popular extensions include:

  • Language support: VSCode" includes built-in support for a wide range of programming languages, including C#, C++, Java", JavaScript, Python", and many more. You can also install additional language extensions to support other languages or add extra features for specific languages.
  • Debugging: VSCode" includes a powerful debugging tool that allows you to set breakpoints, inspect variables, and step through code to find and fix errors.
  • Source control: VSCode integrates with popular source control systems such as Git", allowing you to easily manage your codebase and collaborate with others.
  • Integrated terminal: VSCode" includes an integrated terminal window, allowing you to run command-line tools and scripts directly from within the editor.

Overall, VSCode" is a powerful and flexible code editor that is well-suited for a wide range of development tasks and languages.

C# Brackets

In the C# programming language, brackets (also called curly braces) are used to enclose blocks of code or to define the scope of a code block.

public void ExampleMethod()
    // code goes here

Brackets are also used to define the body of a loop or conditional statement:

for (int i = 0; i < 10; i++)
    // code goes here

if (condition)
    // code goes here
    // code goes here

In C#, it is important to use brackets consistently and correctly to clearly define the scope of a code block and to prevent errors or unintended behavior in your code.

VSCode Prettier

Prettier is a popular code formatting tool that can be used with Visual Studio Code" (VSCode") to automatically format and style your code according to a set of predefined rules. Prettier can help you enforce a consistent code style across your team or project, and can save you time by automatically formatting your code as you write it.

To use Prettier with VSCode", you will need to install the Prettier extension from the VSCode" Marketplace. Once the extension is installed, you can configure Prettier to automatically format your code on save or on demand by modifying the “editor.formatOnSave” and “editor.formatOnType” settings in your VSCode" settings.

To format your code with Prettier in VSCode", you can use the following options:

  • Press the “Shift + Alt + F” key combination to format the current document or selection.
  • Right-click the text editor and select “Format Document” or “Format Selection” from the context menu.
  • Use the “Format Document” or “Format Selection” commands in the “Edit” menu.

Prettier supports a wide range of programming languages, and you can customize the formatting rules and options to fit your specific needs. You can also use Prettier in combination with other formatting tools, such as ESLint, to ensure that your code adheres to a set of agreed-upon style guidelines.

VSCode Format Curly Brackets On The Same Line C#

    private void OnTriggerExit(Collider other)
        if (other.gameObject.tag == "Enemy")
            hitByEnemy = true;

Personally, this frustrated me because the code was becoming less and less readable for me. A number of lines of code shot off into space. Personally, I use Prettier – Code formatter, but it defaults to not having that set for C # so that the opening brace is on the same line. I also did not find an option in the configuration of this extension to change it.

Therefore, you need to move to the c # compiler level. Configurations can be applied locally by creating the omnisharp.json file in the main project directory, or applied globally. In this article, we will configure the first option.

#1 Method In VSCode: omnisharp.json

In the project root directory, create a new omnisharp.json file and then enter the following configuration:

    "FormattingOptions": {
        "NewLinesForBracesInLambdaExpressionBody": false,
        "NewLinesForBracesInAnonymousMethods": false,
        "NewLinesForBracesInAnonymousTypes": false,
        "NewLinesForBracesInControlBlocks": false,
        "NewLinesForBracesInTypes": false,
        "NewLinesForBracesInMethods": false,
        "NewLinesForBracesInProperties": false,
        "NewLinesForBracesInObjectCollectionArrayInitializers": false,
        "NewLinesForBracesInAccessors": false,
        "NewLineForElse": false,
        "NewLineForCatch": false,
        "NewLineForFinally": false

If you do not see the difference in formatting right away, close and restart VSCode". And now I present how the previously presented method looks after applying the new formatting. (VSCode" format curly brackets on the same line c#)

Only on such a small method we limited the code by two lines! Think how much less code will take up a line when the class is more complex? 🙂

    private void OnTriggerEnter(Collider other) {
        if (other.gameObject.tag == "Enemy") {
            hitByEnemy = true;

#2 Method

In Visual Studio Code" (VSCode"), you can use the built-in formatting feature to automatically format your code, including the placement of curly brackets. To do this, follow these steps:

  1. Open" your C# code file in VSCode".
  2. Press the “Shift + Alt + F” key combination to format the code.

By default", VSCode" will use the default" formatting settings for C#, which include placing the opening curly bracket on the same line as the control statement, and the closing curly bracket on a new line.

You can customize the formatting settings for C# in VSCode" by modifying the “editor.formatOnType” and “editor.formatOnSave” settings in the settings.json file. For example, to have the opening curly bracket always placed on a new line, you can add the following setting:

"editor.formatOnType": {
    "openBrace": true

You can also use the “Format Document” and “Format Selection” commands in the “Edit” menu to format specific parts of your code.


Keep in mind that the formatting rules and preferences may vary among developers, so it is a good idea" to discuss and agree on a consistent formatting style with your team or project.

That’s all about topic: VSCode" format curly brackets on the same line c#! Enjoy!

Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 808

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?