简体   繁体   中英

How do I make property accessors all on one line, including closing braces, in Visual Studio 2017, both when authoring and refactoring?

The Encapsulate Field refactoring produces very spread out accessors:

    private int steps= 6;

    public int Steps
    {
        get
        {
            return steps;
        }

        set
        {
            steps= value;
        }
    }

I found how I could change the first brace by going to: Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines -> New Line Options for braces and unchecking: "Place open brace in a new line for property, indexer, and event accessors"

Now I get this:

private int steps= 6;

public int Steps
{
    get{
        return steps;
    }

    set{
        steps= value;
    }
}

But I want to go a step further and end up with this:

private int steps= 6;

public int Steps
{
    get { return steps; }
    set { steps= value; }
}

How do I convince Visual Studio that it should be even more compact?

EDIT: The propfull snippet is great (thanks @popsiporkkanaa), but I can't use that to automatically refactor existing private members to properties.

In Visual Studio there is a snippet of "propfull", which will offer you this:

   public int MyProperty
   {
        get { return myVar; }
        set { myVar = value; }
   }

For compact accessors, check out Code Style > General > Expression preferences > Use expression body for accessors = "When possible" or "When on single line".

Then when you refactor with "Encapsulate field" it will return

public int Steps { get => steps; set => steps = value; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM