Make visual studio do spaces before and after '=' - c#

Always when I finish a statement I write ; and it formats the statement, like 1+2+3 to 1 + 2 + 3. But it doesn't do it to x=y to x = y. Why? How to make it to do that?
I read everything in Tools -> Options -> Text Editor -> C# -> Formatting -> Spacing and didn't saw anything that is even related to =.

You can try Resharper. It provides much more features in code editing. also you can press Ctrl + KD to make formatting.

Related

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)

How to make Visual Studio not put { on a new line?

If I have code like this
if(true){
and I add
}
it is transformed into
if (true)
{
}
but I would like it to stay in format
if (true) {
}
Even if I copy code that has if like this, it is transformed into the longer version.
P.S. I understand that it's a C++ and C# standard to use the longer version, however I do come from a different standard, and it's easier for me to use the shorter version.
I bet there is some formatter option that can be turned off or changed.
Go to Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting-> New Lines
Here there are a lot of options. Remove the check on all options here to never put the open bracket on a new line.
EDIT
The section New Line Options for expressions does not relate to placement of code in conjunction with brackets, so those you don't need to touch.
Bear in mind that if you're using Resharper the above procedure won't help, since Resharper overrides the native VS behavior. If that's your case go to Resharper > Options, Code Editing > C# > Formatting Style > Braces Layout, set all top "Braces Layout" options to "At end of line (K&R style)".
Took me a while to figure this out, so I hope it helps someone.
For all the Mac Users out there, this is how I solved it:
Preferences -> Source Code -> Code Formatting -> C# source code -> C# Format -> Edit
There you can change it under the Category: New Lines
You can change that in the options of your Visual Studio. Go to Tools -> Options -> Text Editor -> C# -> formatting -> new lines (or something similar, I only have the german version of visual studio). You can then change where you want to put the brackets into the next line and where you don't. Personally, I removed all the options, but if you want to have a new line e.g. in loops, there are many options to customize this.
Have a look at Tools -> Options then Text Editor -> C# -> Formatting -> New Lines
For C++, the only way I can avoid VC++ putting '{' on new line for namespaces is by unchecking "Automatically format block when I type a }" option:
To do the same thing for CSS you have to go to: Tools > Options > Text Editor > CSS > Advanced > Formatting > Automatic Formatting > Off

How do I stop Visual Studio from inserting "object" when I type "new {"

When editing a C# source file, I type
new {
Visual Studio auto-corrects it to
new object{
Is there a way to stop this behavior?
You can configure which characters being typed commit the current intellisense selection. In Tools | Options | Text Editor | C# | IntelliSense.
Remove "{" and ensure committed by the space bar is not checked.
NB. This option is no longer present as of Visual Studio 2015.
I ran into this issue, and the answers above didn't work for me. In my case, it was caused by Resharper, and I addressed it by navigating to Resharper -> Options -> Environment -> Intellisense -> Completing Characters and adding the opening curly brace character "{" to the "Do not complete on" list for C#.
What are you typing before the new {?
I've just tried it and it auto-completes with the object type, so if I type:
Button test = new {
it becomes:
Button test = new Button{
But if I type:
var test = new {
it leaves it alone.
I haven't configured my VS2008 install in any way.
Have you checked your auto-complete options for ReSharper? I just tried this in a new (empty) class with the default ReSharper settings and couldn't duplicate it. What version of studio/ReSharper are you using?
Try typing the left brace first, then going back and typing new. VS2008 does this for me (without ReSharper) and, if I remember, this is what I do. It's less typing than deleting the inserted "object".

Categories