Formatting C# - single line if with braces

According to the 'official' (afaik) C# code convention
, single line if
statements are allowed, but the braces are required.

if (foo) { bar(); }

I'm not sure how to configure R# to do this. Everywhere else, I've got
it set to put braces on the next line and all that ... and I do see a
setting for allowing property getters/setters on a single line ... but
when I've got one-liner if statements with braces, R# wants to transform
it to multi-line.

2 comments
Comment actions Permalink

Hello Chris,

From your reference:
begin
2.3 Single line statements
Single line statements can have braces that begin and end on the same line.

public class Foo
{
int bar;

public int Bar
{
get { return bar; }
set { bar = value; }
}

}

It is suggested that all control structures (if, while, for, etc.) use braces,
but it is not required.
end


According to the 'official' (afaik) C# code convention
, single line if
statements are allowed, but the braces are required.

if (foo) { bar(); }

I'm not sure how to configure R# to do this. Everywhere else, I've got
it set to put braces on the next line and all that ... and I do see a
setting for allowing property getters/setters on a single line ... but
when I've got one-liner if statements with braces, R# wants to
transform it to multi-line.



0
Comment actions Permalink

Matt Moody wrote:

From your reference:
begin
2.3 Single line statements
Single line statements can have braces that begin and end on the same line.

public class Foo
{
int bar;

public int Bar
{
get { return bar; }
set { bar = value; }
}

}

It is suggested that all control structures (if, while, for, etc.) use
braces, but it is not required.
end


Ha! I hadn't read that far. In the section immediately prior it says,
"Braces should never be considered optional. Even for single statement
blocks, you should always use braces."

Weird.

But ... this post isn't about the standard (I just linked to it in case
it clarified my question) ... the question is -- if I want braces on a
single line, how do I configure R# to help me in this regard, cuz most
of the time it changes this:

if (foo) { bar(); }

to this:

if (foo)
{
bar();
}

...and I can't see where to configure R# to stop changing my code for me
in that case. In "Code Style | C# | Line Breaks and Wrapping" there's
"Place simple accessor on single line" -- I'm looking for something
similar for if statements.

0

Please sign in to leave a comment.