Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

301
Vistas
How to customize formatting of code that is generated by "Encapsulate Field"?

Previously I am fairly certain that the "Encapsulate Field" command would turn something like the following:

public int SomeNumber;

into the following (what I want from VS 2015):

private int someNumber;

public int SomeNumber {
    get { return someNumber; }
    set { someNumber = value; }
}

but in Visual Studio 2015 I am seeing the following:

private int someNumber;

public int SomeNumber {
    get {
        return someNumber;
    }

    set {
        someNumber = value;
    }
}

Is there a way to fix this?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

This was a design change in VS2015. In previous versions, the refactoring command paid attention to the Tools > Options > Text Editor > C# > Wrapping > "Leave block on single line" option. With it turned on, you'll get the property getter and setter body the way it encoded in the snippet, braces on the same line. The way you like it.

Different in VS2015, it now pays attention to the Tools > Options > Text Editor > C# > Formatting > New Lines > "Place open brace on new line for methods" setting. You get to choose between "egyptian" braces or having the opening brace separate. Neither of which you like.

Accidents happen when Microsoft creates new VS versions, this was not an accident. Whether this was done by "popular demand" is hard to reverse-engineer, I consider it pretty likely since this refactoring is usually done to write a non-trivial getter or setter, the kind that won't fit a single line. Providing us with a choice between all three possible formatting preferences looks like a problem to me, the existing formatting options are not a good match.

Only real option is to let Microsoft know that you are not happy with the change. There is an existing UserVoice article that proposes a change. You can vote for it or write your own. Post a link to it in your question so other SO users can vote.

over 4 years ago · Santiago Trujillo Denunciar

0

Will that help you:
-Encapsulate Field Refactoring (C#): https://msdn.microsoft.com/en-us/library/a5adyhe9.aspx

This website seems to offer a solution a the end of the topic.

Check also this post : -Different Refactoring style for field encapsulation: how to make Visual Studio change it? Different Refactoring style for field encapsulation: how to make Visual Studio change it?

This one is corresponding to your question in a certain manner: the question is really related to yours :)

over 4 years ago · Santiago Trujillo Denunciar

0

I myself have tried changing the snippet file to suite my file but VS doesn't take effect. What I end up doing is

  1. Encapsulate the fields as usual.
  2. Copy and paste the code into notepad++ and do a Find and Replace.

Find:

(\{)*(\s*)*(get|set)\r\n\s+{\r\n\s+(.*)\r\n\s+\}\s+

Replace with:

$1$3\{$4\}
  1. Paste the result back into VS. VS will format it follow the "Leave block on single line".
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda