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

You are currently viewing VSCode format curly brackets on the same line c# – check simple solution in 2 mins!
Share This Post, Help Others, And Earn My Heartfelt Appreciation! :)
4.9
(807)

[ 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:

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.

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

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

If you enjoyed this post please add the comment below and share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 807

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

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments