Resolved Make code reformat automatic?

PickyBiker

New member
Joined
May 15, 2020
Messages
4
Programming Experience
Beginner
After years of using VB.net I became quite fond of the automatic reformat feature at each line return. Now that I am learning C#, I find that I can reformat the code with ctrl+k+d, but I don't seem to be able to find a way to format the code automatically after each return.

How can I make that automatic?
 
The C# code editor won't work the same way as the VB code editor in that regard. I use ReSharper so I often don't know what is being done by VS and what by ReSharper but, if I'm not mistaken, the C# code editor will format a line when you type a semicolon and a block when you type a closing brace. If a line or block is not formatted, e.g. because you made an edit in the middle, try typing over the semicolon or brace at the end of it. I don't think that you have to select it as the code editor is smart enough to overtype in that scenario, but that might be ReSharper and not VS.
 
I found that it does work on blocks, and semicolons, just not on returns even though that option is checked. Guess I'll just accept that's the way it works.
 
Can you show us an example of what you are typing in and then pressing "Return"? What is your expected formatting? What formatting (or lack of formatting) are you currently seeing?
 
In the code below, the first two lines in the Update are out-dented. If I go to the end of the platform.transform line and hit return, it gives a new line but the formatting doesn't change. In VB, that block would have been re-formated.

If I go to the end of the first line in the Update sub and hit return, no format change, just a new line.
If I erase the ';' at the end of the first line and type it again, that line does reformat but the second line remains outdented.
if I erase the closing brace on the Update sub and retype it, everything in the block gets reformatted.

In fact, I don't think VB would have allowed the first two lines to be outdented in the first place. As soon as you left the line with a mouse click or return, it woluld have been reformatted.

Guess there must be a reason for the difference in behavior.

C#:
using UnityEngine;
public class Tilt : MonoBehaviour
{
    public float xAngle, zAngle;
    private GameObject platform;

    void Update()
    {
     platform = GameObject.Find("Cube");
    float xAngle = Input.GetAxis("Vertical") * .25f;
        float zAngle = -Input.GetAxis("Horizontal") * .25f;
        platform.transform.Rotate(xAngle, 0, zAngle, Space.Self);
    }
}
 
The code editors were created by different teams. VB is aimed far more at beginners and thus tries to make more things easy. C# is aimed more at experienced developers and particularly those with experience in C-based languages. Those people are far more likely to want to do things their own way.

I'm not 100% sure how the Automatically format on return option works but here's something I just tried and what happened. I entered this code:
C#:
for(var i=0;
    i<10;
    i++)
{}
As I hit Enter at the end of each line, no formatting was applied. When I entered the opening brace, the closing brace was added automatically and the caret was placed between the braces but no formatting was applied. When I hit Enter, this was the result:
C#:
for (var i = 0;
     i < 10;
     i++)
{

}
As you can see, formatting was applied when I hit Enter, and the caret was placed on the line between the braces and indented one tabstop. Again, I'm not sure whether ReSharper is responsible for any of this but that may well be what they mean about formatting on return.

It's also worth noting that, if I typed a closing brace instead of Enter, I got this:
C#:
for (var i = 0;
     i < 10;
     i++)
{
}
with the caret placed after the closing brace.
 
Back
Top Bottom