VS 2022 Style Rule to keep Auto-Implemented Properties on One Line - c#

When I cut & paste the following C# text in VS 2022:
public int? Units { get; set; }
it pastes it formatted as:
public int? Units
{
get; set;
}
What .editorconfig rule needs to be adjusted to tell VS that I want my auto-implemented properties to remain on a single line?
Also, for heartfelt bonus points (but no actual points), is there a good way of finding this information myself? I googled this until my eyes bled, and I searched the options under Options -> Text Editor -> C# -> Code Style for as long as I could, with no luck.

In the .editorconfig UI, it is:
Whitespace -> Wrapping preferences -> Leave block on single line (checked)
In the .editorconfig file, add:
csharp_preserve_single_line_blocks = true
While this question is now answered, I still find myself frustrated trying to figure out how to find these options in these instances when I can't think of the right word or phrase to search by. To a certain extent it just requires luck, which does not make for a happy code monkey.

Related

VS "format document" setting to respect author's newline choices?

Normally my properties look like this ("Version 1"):
public string Foo {get; set;}
I am fine with this. But sometimes my names get long, and it's not so good ("Version 2"):
public TypeWithALongName<AnotherLongNameInAngleBrackets> ThePropertyHasALongNameTooAndItsTooWideForTheScreen {get; set;}
When the name gets so long that the line isn't visible on the screen, it's not so good. I'd like to mitigate this by inserting a newline ("Version 3"):
public TypeWithALongName<AnotherLongNameInAngleBrackets>
ThePropertyHasALongNameTooAndItsTooWideForTheScreen {get; set;}
Now I can read it again! I like this. But VS does not. Format Document (control-K, control-D) now changes it to this ("Version 4"):
public TypeWithALongName<AnotherLongNameInAngleBrackets>
ThePropertyHasALongNameTooAndItsTooWideForTheScreen
{get; set;}
I want a way to ask VS to respect my newline choices in this area! If I use any of versions 1-4, Format Document should leave it alone. Is that possible?
If you want that behaviour. You can change the following setting.
Go to Tools->Options.
In the Options List. Navigate to Text Editor -> C# -> Code Style -> Formatting -> New Lines
Uncheck the box for "Place open brace for properties, indexers, and events".
This will give you the behaviour you want.
Checkout editorconfig if you want your styles to be shared with the dev team.
EditorConfig in VS 2022

If else formatting Visual Studio not indenting correctly

So this is going to be a kind of strange question, I have never run into this formatting issue with any other programs I have written. I think it is because my if statements has multiple || which needed multiple lines to make it appear more readable.
Notice the dotted line under my else if(). It is getting shifted right for some reason under the if instead of the else.
Is there some kind of formatting setting that can fix this? Or is there a different way I am supposed to do this else if statement? Having all those OR checks is pretty much necessary but I did not wan't to put them all on a single line as that would look real ugly.
Thanks!
Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting -> New Lines
Uncheck everything and save.
Then, CTRL + E, D to format the document.
Also, you're using a huge compound if statement there, which is quite frankly a mess. If you need to check multiple items as starting values, put them all into a list.
List<String> ModelNumberPrefixes = new List<String>();
ModelNumberPrefixes.Add("A1C1C");
ModelNumberPrefixes.Add("A1C1D");
//etc
ModelNumberPrefixes.ForEach(s => {
if (ModelNumber.StartsWith(s)) {
//Whatever you need to do in your big if block
}
});
To format a selection: Ctrl+K, Ctrl+F
To format a document: Ctrl+K, Ctrl+D
See the pre-defined keyboard shortcuts . (These two are Edit.FormatSelection and Edit.FormatDocument.)

Format document in Visual Studio to insert braces?

Is there a way to "Format document" in Visual Studio to insert braces around single-statement blocks for C# code? For example this:
if (x)
y();
... would become something like:
if (x) { y(); }
The auto formatting seems to deal with indentation but not this brace insertion. Is there a way to do it?
Actually, there seems to be something built-in to Visual Studio to do this.
If you go to Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines and make sure that you have Place open braces on new lines for control block checked.
Then, go to your document and use the key combination CTRLKD, this should reformat your document and add the curly braces.
In case you have resharper you can configure it to force braces depending on your criteria.
Than in the existing code press ctrlaltshiftf, it will format whole file. Or select just part of the code, in this case resharper will format just selection
P.S. ctrlaltf opens clean up dialog. You can configure cleanup options.
I gave a vote to Gimly's answer as it is pretty much correct. These things change over time of course. I would have added a comment but I wanted to paste in some images. Location of settings in VS2019 is at:
Uncheck the appropriate check boxes and select OK.
The shortcut key did not work, which is a shame because, I love them! This documentation suggests that you use
CTRL-K, CTRL-E or, you use the broom icon:
.
Neither option worked for me perhaps because, that is not the intention, given the list of options. However, if you mark and cut all your code and then, paste the code back into the file, the new standard is adopted.

Making Resharper put a space in-between class/namespace identifier and the curly brace

I've just installed Resharper on my machine, and by default he presents me with the following C# code formatting:
namespace machineLearning{
public class Class1{
}
}
I've tried fiddling with the different options on Options -> C# -> Formatting Style but I can't seem to find what the option to correct this behaviour is. There seems to be no option explicitly or less-explicitly concerning adding a space between the identifier and the following brace.
How to accomplish that?
In Resharper 7 It is under Resharper -> Manage Options -> Code Editing -> C# -> Formatting Style -> Braces Layout -> Method declaration -> (set the value to ) At End of line (K & R Style)

Visual studio formatting issue

I am using Visual Studio 2008. In my editor whenever I write an automatic property it's "get" and "set" wraps to a single line whenever I wrote "set" and put semicolon(;). like this:
public string MyProperty
{
get; set;
}
I want it to be formatted like this
public string MyProperty
{
get;
set;
}
Currently I am manually formatting it to this after writing it. How can I set it as a default format?
Edit:
Options > Text Editor > C# > Formatting > Wrapping > Leave block on single line is already unchecked for me.
I unchecked all three option available in Options > Text Editor > C# > Formatting > General, but it doesn't work. Anything else?
If you put it all on one line, the default formatting options will leave it alone:
public string MyProperty { get; set; }
This is controlled by:
Options > Text Editor > C# > Formatting > Wrapping > Leave block on single line
If you really want to format it your way, you could disable:
Options > Text Editor > C# > Formatting > General > Automatically format completed block on }
But you'd probably want to disable Automatically format on paste too.
If you're using ReSharper, Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping has an option "Place abstract/auto property/indexer/event on single line" that controls the behavior you describe.
Tools -> Options -> Text editor has a lot of options for various languages as to how Visual Studio should (or should not) auto-format your code.
Look under Tools -> Options -> Text Editor -> C# -> Formatting.
You might find a setting there that will format it how you want.
EDIT
This is a workaround, but it would do the trick.
Make a code snippet for automagic properties. Here is link with more info on how to do that. It will need a little modification, but you can handle it. ;)
http://msmvps.com/blogs/kevinmcneish/archive/2007/04/30/property-code-snippet-for-visual-studio-2005.aspx
Do you have coderush or any other code generating addons installed?
Note - I believe this was https://github.com/dotnet/roslyn/issues/2837 and fixed in VS2015 Update 1.

Categories