Joe
Joe

Reputation: 11677

Hide curly braces in C#

After getting back into Python, I'm starting to notice and be annoyed more and more by my C# coding style requiring braces everywhere

 if (...)
 {
     return ...;
 }
 else
 {
     return ...;
 }

preferring the (subjective) much cleaner looking python counter-part

if ...:
    return ...
else
    return ...

is there any way I can go about hiding these braces (as they do take up about 30% of my coding screen on average and just look ugly!)

Upvotes: 8

Views: 1084

Answers (10)

YellPika
YellPika

Reputation: 2902

If you're feeling really evil:

if (iAmEvil)                                                                                                             {
    LookMa();
    NoBraces();
    OrSemiColonsForThatMatter()                                                                                         ;}
else {
    IfYouDontGetIt();
    ScrollRight();
}

Of course, it's probably more trouble than it's worth.

Upvotes: 1

explorer
explorer

Reputation: 12090

C/C++/Java/C# folks have an ages old racist kind of family feud with the Cobol/BASIC/Python clan. It's a real either/or thing... No best-of-the-both-world's, having-cakes-and-eating-them-too fun allowed :)

Upvotes: 0

Arto Viitanen
Arto Viitanen

Reputation: 188

If you need .NET but like Python syntax, you might like to check Boo.

Upvotes: 1

Dhaivat Pandya
Dhaivat Pandya

Reputation: 6536

Check out scribes, its a really nice editor that is customizable to a large extent, and I'm quite sure that you can do what you needed with it.

Upvotes: -2

Sören
Sören

Reputation: 2731

  1. You don't need the brackets if you have only one statement. For example, the following is working:

    for (int i = 0; i < 10; i++)
        if (i == 9)
            break;
    

(even though that code does not make any sense)

  1. You can change the settings in Visual C# for formatting C# code as Skilldrick says, to make the code shorter:

Go to Tools -> Options -> Text Editor -> C# -> formatting (or something similar, I only have the german version of visual studio).

Hope that helps

Upvotes: 0

Moo-Juice
Moo-Juice

Reputation: 38800

Sorry, but if you're coding in C# and doing more than just simple single-expression blocks, you're going to have to suck it up. Python's "indent-denotes-scope" grammar may be nice, but it's Python, not C#.

Upvotes: 10

Marlon
Marlon

Reputation: 20312

if (condition) {
    statement1
    statement2; }
else {
    statement3;
    statemen4; }

much uglier in my opinion :(

Upvotes: 2

Pekkasso
Pekkasso

Reputation: 419

For single statements you do not need the curly braces. For times you need them you might be OK with placing the braces like:

if(expr) {
    ...
} else {
    ...
}

Upvotes: -1

LukeH
LukeH

Reputation: 269478

Well, you can omit the braces in C#, so long as the if and else "blocks" are just single statements:

// perfectly legal
if (...)
    return ...
else
    return ...

If you need multi-line if and else blocks then you're stuck with braces.

Upvotes: 0

Skilldrick
Skilldrick

Reputation: 70859

You could switch to:

 if (...) {
     return ...;
 } else {
     return ...;
 }

to gain some screen.

I think it's a bad idea to pretend that C# works like Python though.

Upvotes: 5

Related Questions