C
C#4mo ago
Faker

Inline if statement best practices in C#

When do we need to use inline statement in C# for best practices pls. For example, consider the if statement below with the condition of the if statement being writen on the same line. Why can we just write the break statement and omitting the curly braces pls. When is that considered a good practice? Say we have multiple if elseif statement, if one of the if else-if statement isn't an "inline" syntax, then every if statement shouldn't be inline ?
C#
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
if (i == 7) break;
}
C#
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
if (i == 7) break;
}
4 Replies
ero
ero4mo ago
personal preference. i personally don't even allow any statements without braces.
Faker
FakerOP4mo ago
yep noted, I also prefer to be as explicit as possible with all the curly braces and each statement on their lines
many things
many things4mo ago
i think convention says to always use { }, but personally i've never had issues when not using them, so one line return guard clauses for me are allowed to not have {}
ero
ero4mo ago
csharp_prefer_braces = when_multiline

Did you find this page helpful?