0

We are currently using C# and want to know if C# bracket placements can change the results.

In Javascript, it matters as results vary based on the curly brace placement .

Why do results vary based on curly brace placement?

In JS they should be kept on the same line, if there are problems with browsers incorrectly interpretting it.

if (x == a)
{
    ...
}

if (x == a) {
    ...

Does bracket placement matter for C#?

mattsmith5
  • 540
  • 4
  • 29
  • 67
  • 5
    No, they don't. – ProgrammingLlama Jun 08 '21 at 01:50
  • 3
    Opinionated side note: I'd recommend following Microsoft's style of writing code (some of it is documented in [this style guide](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions)) when writing your own code in C#, so as to make your code easy to follow by other C# developers. With that in mind, I would expect the brace to be on a new line in the sample you provided. Of course this is ultimately up to you, but that's just my 2 cents. – ProgrammingLlama Jun 08 '21 at 02:00
  • 1
    If you want to get in a fight with a bunch of programmers, tell them that their indentation and brace rules are wrong: https://en.wikipedia.org/wiki/Indentation_style – Flydog57 Jun 08 '21 at 02:07
  • 1
    @Flydog57 Now if it was Python, nobody would disagree on the indentation front. :D – ProgrammingLlama Jun 08 '21 at 02:12
  • I didn't ask for recommendation, but whether it was cause any effect or not, we had legacy coders who were using both styles of brackets, and we are trying to analyze the effects on our team @Llama – mattsmith5 Jun 08 '21 at 06:16
  • @OlivierRogier The question OP linked shows that brace placement can have measurable effects in some Javascript implementations (specifically disregarding an entire block because `return {` results `return` as being interpreted as `return; {`). OP was asking if such an effect exists in C#. That's certainly not asking for any kind of recommendation or at all opinion-based. – ProgrammingLlama Jun 08 '21 at 06:20
  • @llama It's quite obvious that I didn't interpret your question as soliciting recommendations. I even went as far as to say "opinionated _side note_" (i.e. tangential, and opinion). I simply meant that, given it has no effect, my personal recommendation is that you follow similar coding styles to Microsoft when working with C#. That's it. – ProgrammingLlama Jun 08 '21 at 06:26
  • @Llama agreed, not sure who closed this question, I would vote to reopen it – mattsmith5 Jun 08 '21 at 15:47
  • There are currently two votes to reopen (one is mine). So you should just need one more person to vote to reopen it. – ProgrammingLlama Jun 08 '21 at 16:01

1 Answers1

6

No, they don't.

In JavaScript, you can write code without ending your lines of code with a semicolon, and JavaScript will automatically fill in the missing semicolons when it interprets your code. That's what this answer to the question you linked is essentially stating. That is to say: the brace placement isn't the real issue in JS; it's the ability to write code with/without semicolons and have JS automatically fill these in for you. The brace placement issue is more of a side effect of this functionality.

In C#, a "line" doesn't end until the semicolon is reached (even if that "line" spans multiple physical lines), and writing code without semicolons isn't something that is automagically taken care of for you by the compiler; it will simply fail to compile. The brace placement in C# therefore is unimportant.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86