Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2017 C# Formatting: One-Line If Statements

In Visual Studio 2017 for C#, is there a way to leave one-line if statements on one line without affecting the formatting of other control blocks? The "Place open brace on new line for control blocks" rule under "Text Editor > C# > Code Style > Formatting > New Lines" will allow if statements to remain on one line, but it will also force all other control blocks to have the curly bracket on the same line.

For example, I'd like the following statement's formatting to be left alone:

if(x == null) { return; }

Instead of being auto-formatted to this:

if(x == null)
{ return; }

While also allowing other control statements to keep their curly bracket on the next line like this:

foreach(string s in strings)
{
    ....
}
like image 291
Matt Kemmer Avatar asked May 03 '17 19:05

Matt Kemmer


People also ask

Can u use Visual Studio for C?

You can use C++ in Visual Studio to create anything from simple console to Windows desktop apps, from device drivers and operating system components to cross-platform games for mobile devices, and from small IoT devices to multi-server computing in the Azure cloud.

Is Visual Studio C free?

Visual Studio Community. A fully-featured, extensible, free IDE for creating modern applications for Android, iOS, Windows, as well as web applications and cloud services.

What's new in C++17 in Visual Studio 2017?

For detailed information, see C++ Conformance Improvements in Visual Studio 2017. The compiler supports about 75% of the features that are new in C++17, including structured bindings, constexpr lambdas, if constexpr, inline variables, fold expressions, and adding noexcept to the type system. These features are available under the /std:c++17 option.

How to install C++ in Visual Studio 2015?

Run the downloaded setup program and choose Custom installation and then choose the C++ component. To add C and C++ support to an existing Visual Studio 2015 installation, click on the Windows Start button and type Add Remove Programs.

Where can I find the Visual Studio 2017 IDE?

] C:\Program Files (x86)\Microsoft Visual Studio\2017\Community> Without doubt one of the best features of Visual Studio is the convenient IDE. Although it takes more configuring, you get bonuses such as basic debugging before compiling (for example if you forget a ;)

What is Microsoft Visual C++ redistributable for Visual Studio 2017?

Microsoft Visual C++ Redistributable for Visual Studio 2017. This package installs run-time components of Visual C++ libraries and can be used to run such applications on a computer even if it does not have Visual Studio 2017 installed.


1 Answers

Yes, as suggested by elgonzo's comment that behavior is controlled by going to the "Tools" menu, then Options -> Text Editor -> C# -> Code Style -> Formatting -> Wrapping

Check "Leave statement and member declarations on the same line"

like image 172
Patrick Avatar answered Sep 22 '22 17:09

Patrick